authorgravatar for kiyonnxperiaz@gmail.comTemariVirus <kiyonnxperiaz@gmail.com> 2025-12-13 05:34:12+08:00
committergravatar for kiyonnxperiaz@gmail.comTemariVirus <kiyonnxperiaz@gmail.com> 2026-03-16 15:42:35+08:00
loge4985509520445e3fb7c765149863b4ce6121738
treea1463d2fb655751289612e7f73e2cfb368d7fe89
parent06d9ab7121c30e4142e48908ed8ef79c3387ad30
signaturebadge-check Signed by SSH key SHA256:+zAK94uwewsXTNmaLt3PC6gO2ZcmUQey3+5ib4pNN3k

std.math.pow: organise and add special case tests

Added tests for special cases which were previously untested. Updated comments to use the same language as IEEE 754-2008.

1 files changed, 73 insertions(+), 32 deletions(-)

lib/std/math/pow.zig+73-32
......@@ -13,13 +13,13 @@ const expect = std.testing.expect;
1313/// - pow(x, +-0) = 1 for any x
1414/// - pow(1, y) = 1 for any y
1515/// - pow(x, 1) = x for any x
16/// - pow(nan, y) = nan
17/// - pow(x, nan) = nan
16/// - pow(nan, y) = nan for any y != 0
17/// - pow(x, nan) = nan for any x != 1
1818/// - pow(+-0, y) = +-inf for y an odd integer < 0
1919/// - pow(+-0, -inf) = +inf
2020/// - pow(+-0, +inf) = +0
2121/// - pow(+-0, y) = +inf for finite y < 0 and not an odd integer
22/// - pow(+-0, y) = +-0 for y an odd integer > 0
22/// - pow(+-0, y) = +-0 for finite y an odd integer > 0
2323/// - pow(+-0, y) = +0 for finite y > 0 and not an odd integer
2424/// - pow(-1, +-inf) = 1
2525/// - pow(x, +inf) = +inf for |x| > 1
......@@ -39,20 +39,20 @@ pub fn pow(comptime T: type, x: T, y: T) T {
3939 @compileError("pow not implemented for " ++ @typeName(T));
4040 }
4141
42 // pow(x, +-0) = 1 for all x
43 // pow(1, y) = 1 for all y
42 // pow(x, +-0) = 1 for any x
43 // pow(1, y) = 1 for any y
4444 if (y == 0 or x == 1) {
4545 return 1;
4646 }
4747
48 // pow(nan, y) = nan for all y
49 // pow(x, nan) = nan for all x
48 // pow(nan, y) = nan for any y != 0
49 // pow(x, nan) = nan for any x != 1
5050 if (math.isNan(x) or math.isNan(y)) {
5151 @branchHint(.unlikely);
5252 return math.nan(T);
5353 }
5454
55 // pow(x, 1) = x for all x
55 // pow(x, 1) = x for any x
5656 if (y == 1) {
5757 return x;
5858 }
......@@ -77,7 +77,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
7777 }
7878
7979 if (math.isInf(y)) {
80 // pow(-1, inf) = 1 for all x
80 // pow(-1, inf) = 1
8181 if (x == -1) {
8282 return 1.0;
8383 }
......@@ -222,45 +222,86 @@ test pow {
222222}
223223
224224test "special" {
225 const epsilon = 0.000001;
226
225 // pow(x, +-0) = 1 for any x
227226 try expect(pow(f32, 4, 0.0) == 1.0);
228227 try expect(pow(f32, 7, -0.0) == 1.0);
228 try expect(pow(f32, math.nan(f32), -0.0) == 1.0);
229 // pow(1, y) = 1 for any y
230 try expect(pow(f32, 1.0, 4) == 1.0);
231 try expect(pow(f32, 1.0, 7) == 1.0);
232 try expect(pow(f32, 1.0, -math.inf(f32)) == 1.0);
233 try expect(pow(f32, 1.0, math.nan(f32)) == 1.0);
234 // pow(x, 1) = x for any x
229235 try expect(pow(f32, 45, 1.0) == 45);
230236 try expect(pow(f32, -45, 1.0) == -45);
237 try expect(math.isPositiveZero(pow(f32, 0.0, 1.0)));
238 try expect(math.isNegativeZero(pow(f32, -0.0, 1.0)));
239 try expect(math.isPositiveInf(pow(f32, math.inf(f32), 1.0)));
240 try expect(math.isNan(pow(f32, math.nan(f32), 1.0)));
241 // pow(nan, y) = nan for any y != 0
231242 try expect(math.isNan(pow(f32, math.nan(f32), 5.0)));
232 try expect(math.isPositiveInf(pow(f32, -math.inf(f32), 0.5)));
233 try expect(math.isPositiveInf(pow(f32, -0.0, -0.5)));
234 try expect(pow(f32, -0.0, 0.5) == 0);
243 // pow(x, nan) = nan for any x != 1
235244 try expect(math.isNan(pow(f32, 5.0, math.nan(f32))));
245 // pow(+-0, y) = +-inf for y an odd integer < 0
236246 try expect(math.isPositiveInf(pow(f32, 0.0, -1.0)));
237 //expect(math.isNegativeInf(pow(f32, -0.0, -3.0))); TODO is this required?
247 try expect(math.isNegativeInf(pow(f32, -0.0, -5.0)));
248 // pow(+-0, -inf) = +inf
238249 try expect(math.isPositiveInf(pow(f32, 0.0, -math.inf(f32))));
239250 try expect(math.isPositiveInf(pow(f32, -0.0, -math.inf(f32))));
240 try expect(pow(f32, 0.0, math.inf(f32)) == 0.0);
241 try expect(pow(f32, -0.0, math.inf(f32)) == 0.0);
251 // pow(+-0, +inf) = +0
252 try expect(math.isPositiveZero(pow(f32, 0.0, math.inf(f32))));
253 try expect(math.isPositiveZero(pow(f32, -0.0, math.inf(f32))));
254 // pow(+-0, y) = +inf for finite y < 0 and not an odd integer
242255 try expect(math.isPositiveInf(pow(f32, 0.0, -2.0)));
243256 try expect(math.isPositiveInf(pow(f32, -0.0, -2.0)));
244 try expect(pow(f32, 0.0, 1.0) == 0.0);
245 try expect(pow(f32, -0.0, 1.0) == -0.0);
246 try expect(pow(f32, 0.0, 2.0) == 0.0);
247 try expect(pow(f32, -0.0, 2.0) == 0.0);
248 try expect(math.approxEqAbs(f32, pow(f32, -1.0, math.inf(f32)), 1.0, epsilon));
249 try expect(math.approxEqAbs(f32, pow(f32, -1.0, -math.inf(f32)), 1.0, epsilon));
257 try expect(math.isPositiveInf(pow(f32, 0.0, -5.2)));
258 try expect(math.isPositiveInf(pow(f32, -0.0, -0.5)));
259 // pow(+-0, y) = +-0 for finite y an odd integer > 0
260 try expect(math.isPositiveZero(pow(f32, 0.0, 3.0)));
261 try expect(math.isNegativeZero(pow(f32, -0.0, 5.0)));
262 // pow(+-0, y) = +0 for finite y > 0 and not an odd integer
263 try expect(math.isPositiveZero(pow(f32, 0.0, 2.0)));
264 try expect(math.isPositiveZero(pow(f32, -0.0, 2.0)));
265 try expect(math.isPositiveZero(pow(f32, 0.0, 5.2)));
266 try expect(math.isPositiveZero(pow(f32, -0.0, 0.5)));
267 // pow(-1, +-inf) = 1
268 try expect(pow(f32, -1.0, math.inf(f32)) == 1.0);
269 try expect(pow(f32, -1.0, -math.inf(f32)) == 1.0);
270 // pow(x, +inf) = +inf for |x| > 1
250271 try expect(math.isPositiveInf(pow(f32, 1.2, math.inf(f32))));
251272 try expect(math.isPositiveInf(pow(f32, -1.2, math.inf(f32))));
252 try expect(pow(f32, 1.2, -math.inf(f32)) == 0.0);
253 try expect(pow(f32, -1.2, -math.inf(f32)) == 0.0);
254 try expect(pow(f32, 0.2, math.inf(f32)) == 0.0);
255 try expect(pow(f32, -0.2, math.inf(f32)) == 0.0);
273 // pow(x, -inf) = +0 for |x| > 1
274 try expect(math.isPositiveZero(pow(f32, 1.2, -math.inf(f32))));
275 try expect(math.isPositiveZero(pow(f32, -1.2, -math.inf(f32))));
276 // pow(x, +inf) = +0 for |x| < 1
277 try expect(math.isPositiveZero(pow(f32, 0.2, math.inf(f32))));
278 try expect(math.isPositiveZero(pow(f32, -0.2, math.inf(f32))));
279 // pow(x, -inf) = +inf for |x| < 1
256280 try expect(math.isPositiveInf(pow(f32, 0.2, -math.inf(f32))));
257281 try expect(math.isPositiveInf(pow(f32, -0.2, -math.inf(f32))));
258 try expect(math.isPositiveInf(pow(f32, math.inf(f32), 1.0)));
259 try expect(pow(f32, math.inf(f32), -1.0) == 0.0);
260 //expect(pow(f32, -math.inf(f32), 5.0) == pow(f32, -0.0, -5.0)); TODO support negative 0?
261 try expect(pow(f32, -math.inf(f32), -5.2) == pow(f32, -0.0, 5.2));
282 // pow(+inf, y) = +inf for y > 0
283 try expect(math.isPositiveInf(pow(f32, math.inf(f32), 2.0)));
284 try expect(math.isPositiveInf(pow(f32, math.inf(f32), 0.2)));
285 // pow(+inf, y) = +0 for y < 0
286 try expect(math.isPositiveZero(pow(f32, math.inf(f32), -2.0)));
287 try expect(math.isPositiveZero(pow(f32, math.inf(f32), -0.2)));
288 // pow(-inf, y) = -inf for y an odd integer > 0
289 try expect(math.isNegativeInf(pow(f32, -math.inf(f32), 5.0)));
290 // pow(-inf, +inf) = +inf
291 try expect(math.isPositiveInf(pow(f32, -math.inf(f32), math.inf(f32))));
292 // pow(-inf, -inf) = +0
293 try expect(math.isPositiveZero(pow(f32, -math.inf(f32), -math.inf(f32))));
294 // pow(-inf, y) = +inf for finite y > 0 and not an odd integer
295 try expect(math.isPositiveInf(pow(f32, -math.inf(f32), 4.0)));
296 try expect(math.isPositiveInf(pow(f32, -math.inf(f32), 0.5)));
297 // pow(-inf, y) = -0 for finite y an odd integer < 0
298 try expect(math.isNegativeZero(pow(f32, -math.inf(f32), -3.0)));
299 // pow(-inf, y) = +0 for finite y < 0 and not an odd integer
300 try expect(math.isPositiveZero(pow(f32, -math.inf(f32), -2.0)));
301 try expect(math.isPositiveZero(pow(f32, -math.inf(f32), -5.2)));
302 // pow(x, y) = nan for finite x < 0 and finite non-integer y
262303 try expect(math.isNan(pow(f32, -1.0, 1.2)));
263 try expect(math.isNan(pow(f32, -12.4, 78.5)));
304 try expect(math.isNan(pow(f32, -12.4, -78.5)));
264305}
265306
266307test "overflow" {