diff options
Diffstat (limited to 'lib/std/math')
| -rw-r--r-- | lib/std/math/__rem_pio2.zig | 2 | ||||
| -rw-r--r-- | lib/std/math/__rem_pio2f.zig | 2 | ||||
| -rw-r--r-- | lib/std/math/ceil.zig | 12 | ||||
| -rw-r--r-- | lib/std/math/complex/exp.zig | 4 | ||||
| -rw-r--r-- | lib/std/math/complex/sinh.zig | 4 | ||||
| -rw-r--r-- | lib/std/math/floor.zig | 12 | ||||
| -rw-r--r-- | lib/std/math/fma.zig | 4 | ||||
| -rw-r--r-- | lib/std/math/ldexp.zig | 30 | ||||
| -rw-r--r-- | lib/std/math/round.zig | 18 |
9 files changed, 52 insertions, 36 deletions
diff --git a/lib/std/math/__rem_pio2.zig b/lib/std/math/__rem_pio2.zig index c8cb8fb644..f01d8fe94a 100644 --- a/lib/std/math/__rem_pio2.zig +++ b/lib/std/math/__rem_pio2.zig @@ -7,7 +7,7 @@ const std = @import("../std.zig"); const __rem_pio2_large = @import("__rem_pio2_large.zig").__rem_pio2_large; const math = std.math; -const toint = 1.5 / math.epsilon(f64); +const toint = 1.5 / math.floatEps(f64); // pi/4 const pio4 = 0x1.921fb54442d18p-1; // invpio2: 53 bits of 2/pi diff --git a/lib/std/math/__rem_pio2f.zig b/lib/std/math/__rem_pio2f.zig index 9f78e18d36..5867fb30d9 100644 --- a/lib/std/math/__rem_pio2f.zig +++ b/lib/std/math/__rem_pio2f.zig @@ -7,7 +7,7 @@ const std = @import("../std.zig"); const __rem_pio2_large = @import("__rem_pio2_large.zig").__rem_pio2_large; const math = std.math; -const toint = 1.5 / math.epsilon(f64); +const toint = 1.5 / math.floatEps(f64); // pi/4 const pio4 = 0x1.921fb6p-1; // invpio2: 53 bits of 2/pi diff --git a/lib/std/math/ceil.zig b/lib/std/math/ceil.zig index cf3adcf5b5..686be8e58d 100644 --- a/lib/std/math/ceil.zig +++ b/lib/std/math/ceil.zig @@ -62,6 +62,8 @@ fn ceil32(x: f32) f32 { } fn ceil64(x: f64) f64 { + const f64_toint = 1.0 / math.floatEps(f64); + const u = @bitCast(u64, x); const e = (u >> 52) & 0x7FF; var y: f64 = undefined; @@ -71,9 +73,9 @@ fn ceil64(x: f64) f64 { } if (u >> 63 != 0) { - y = x - math.f64_toint + math.f64_toint - x; + y = x - f64_toint + f64_toint - x; } else { - y = x + math.f64_toint - math.f64_toint - x; + y = x + f64_toint - f64_toint - x; } if (e <= 0x3FF - 1) { @@ -91,6 +93,8 @@ fn ceil64(x: f64) f64 { } fn ceil128(x: f128) f128 { + const f128_toint = 1.0 / math.floatEps(f128); + const u = @bitCast(u128, x); const e = (u >> 112) & 0x7FFF; var y: f128 = undefined; @@ -98,9 +102,9 @@ fn ceil128(x: f128) f128 { if (e >= 0x3FFF + 112 or x == 0) return x; if (u >> 127 != 0) { - y = x - math.f128_toint + math.f128_toint - x; + y = x - f128_toint + f128_toint - x; } else { - y = x + math.f128_toint - math.f128_toint - x; + y = x + f128_toint - f128_toint - x; } if (e <= 0x3FFF - 1) { diff --git a/lib/std/math/complex/exp.zig b/lib/std/math/complex/exp.zig index f2ae28d3fd..ce25025ded 100644 --- a/lib/std/math/complex/exp.zig +++ b/lib/std/math/complex/exp.zig @@ -120,7 +120,7 @@ fn exp64(z: Complex(f64)) Complex(f64) { } test "complex.cexp32" { - const tolerance_f32 = math.sqrt(math.epsilon(f32)); + const tolerance_f32 = math.sqrt(math.floatEps(f32)); { const a = Complex(f32).init(5, 3); @@ -140,7 +140,7 @@ test "complex.cexp32" { } test "complex.cexp64" { - const tolerance_f64 = math.sqrt(math.epsilon(f64)); + const tolerance_f64 = math.sqrt(math.floatEps(f64)); { const a = Complex(f64).init(5, 3); diff --git a/lib/std/math/complex/sinh.zig b/lib/std/math/complex/sinh.zig index ed344999ee..851af3e62e 100644 --- a/lib/std/math/complex/sinh.zig +++ b/lib/std/math/complex/sinh.zig @@ -79,7 +79,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) { if (iy >= 0x7f800000) { return Complex(f32).init(x * x, x * (y - y)); } - return Complex(f32).init(x * math.cos(y), math.inf_f32 * math.sin(y)); + return Complex(f32).init(x * math.cos(y), math.inf(f32) * math.sin(y)); } return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y)); @@ -146,7 +146,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) { if (iy >= 0x7ff00000) { return Complex(f64).init(x * x, x * (y - y)); } - return Complex(f64).init(x * math.cos(y), math.inf_f64 * math.sin(y)); + return Complex(f64).init(x * math.cos(y), math.inf(f64) * math.sin(y)); } return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y)); diff --git a/lib/std/math/floor.zig b/lib/std/math/floor.zig index d6761ba77e..ab5ca3583b 100644 --- a/lib/std/math/floor.zig +++ b/lib/std/math/floor.zig @@ -98,6 +98,8 @@ fn floor32(x: f32) f32 { } fn floor64(x: f64) f64 { + const f64_toint = 1.0 / math.floatEps(f64); + const u = @bitCast(u64, x); const e = (u >> 52) & 0x7FF; var y: f64 = undefined; @@ -107,9 +109,9 @@ fn floor64(x: f64) f64 { } if (u >> 63 != 0) { - y = x - math.f64_toint + math.f64_toint - x; + y = x - f64_toint + f64_toint - x; } else { - y = x + math.f64_toint - math.f64_toint - x; + y = x + f64_toint - f64_toint - x; } if (e <= 0x3FF - 1) { @@ -127,6 +129,8 @@ fn floor64(x: f64) f64 { } fn floor128(x: f128) f128 { + const f128_toint = 1.0 / math.floatEps(f128); + const u = @bitCast(u128, x); const e = (u >> 112) & 0x7FFF; var y: f128 = undefined; @@ -134,9 +138,9 @@ fn floor128(x: f128) f128 { if (e >= 0x3FFF + 112 or x == 0) return x; if (u >> 127 != 0) { - y = x - math.f128_toint + math.f128_toint - x; + y = x - f128_toint + f128_toint - x; } else { - y = x + math.f128_toint - math.f128_toint - x; + y = x + f128_toint - f128_toint - x; } if (e <= 0x3FFF - 1) { diff --git a/lib/std/math/fma.zig b/lib/std/math/fma.zig index 7ef734cf4e..7afc6e557e 100644 --- a/lib/std/math/fma.zig +++ b/lib/std/math/fma.zig @@ -68,7 +68,7 @@ fn fma64(x: f64, y: f64, z: f64) f64 { if (spread <= 53 * 2) { zs = math.scalbn(zs, -spread); } else { - zs = math.copysign(f64, math.f64_min, zs); + zs = math.copysign(f64, math.floatMin(f64), zs); } const xy = dd_mul(xs, ys); @@ -277,7 +277,7 @@ fn fma128(x: f128, y: f128, z: f128) f128 { if (spread <= 113 * 2) { zs = math.scalbn(zs, -spread); } else { - zs = math.copysign(f128, math.f128_min, zs); + zs = math.copysign(f128, math.floatMin(f128), zs); } const xy = dd_mul128(xs, ys); diff --git a/lib/std/math/ldexp.zig b/lib/std/math/ldexp.zig index f8ab237fad..0934244c65 100644 --- a/lib/std/math/ldexp.zig +++ b/lib/std/math/ldexp.zig @@ -57,6 +57,8 @@ pub fn ldexp(x: anytype, n: i32) @TypeOf(x) { } test "math.ldexp" { + // TODO derive the various constants here with new maths API + // basic usage try expect(ldexp(@as(f16, 1.5), 4) == 24.0); try expect(ldexp(@as(f32, 1.5), 4) == 24.0); @@ -73,20 +75,20 @@ test "math.ldexp" { try expect(math.isNormal(ldexp(@as(f128, 1.0), -16382))); try expect(!math.isNormal(ldexp(@as(f128, 1.0), -16383))); // unreliable due to lack of native f16 support, see talk on PR #8733 - // try expect(ldexp(@as(f16, 0x1.1FFp-1), -14 - 9) == math.f16_true_min); - try expect(ldexp(@as(f32, 0x1.3FFFFFp-1), -126 - 22) == math.f32_true_min); - try expect(ldexp(@as(f64, 0x1.7FFFFFFFFFFFFp-1), -1022 - 51) == math.f64_true_min); - try expect(ldexp(@as(f128, 0x1.7FFFFFFFFFFFFFFFFFFFFFFFFFFFp-1), -16382 - 111) == math.f128_true_min); + // try expect(ldexp(@as(f16, 0x1.1FFp-1), -14 - 9) == math.floatTrueMin(f16)); + try expect(ldexp(@as(f32, 0x1.3FFFFFp-1), -126 - 22) == math.floatTrueMin(f32)); + try expect(ldexp(@as(f64, 0x1.7FFFFFFFFFFFFp-1), -1022 - 51) == math.floatTrueMin(f64)); + try expect(ldexp(@as(f128, 0x1.7FFFFFFFFFFFFFFFFFFFFFFFFFFFp-1), -16382 - 111) == math.floatTrueMin(f128)); // float limits - try expect(ldexp(@as(f32, math.f32_max), -128 - 149) > 0.0); - try expect(ldexp(@as(f32, math.f32_max), -128 - 149 - 1) == 0.0); - try expect(!math.isPositiveInf(ldexp(@as(f16, math.f16_true_min), 15 + 24))); - try expect(math.isPositiveInf(ldexp(@as(f16, math.f16_true_min), 15 + 24 + 1))); - try expect(!math.isPositiveInf(ldexp(@as(f32, math.f32_true_min), 127 + 149))); - try expect(math.isPositiveInf(ldexp(@as(f32, math.f32_true_min), 127 + 149 + 1))); - try expect(!math.isPositiveInf(ldexp(@as(f64, math.f64_true_min), 1023 + 1074))); - try expect(math.isPositiveInf(ldexp(@as(f64, math.f64_true_min), 1023 + 1074 + 1))); - try expect(!math.isPositiveInf(ldexp(@as(f128, math.f128_true_min), 16383 + 16494))); - try expect(math.isPositiveInf(ldexp(@as(f128, math.f128_true_min), 16383 + 16494 + 1))); + try expect(ldexp(math.floatMax(f32), -128 - 149) > 0.0); + try expect(ldexp(math.floatMax(f32), -128 - 149 - 1) == 0.0); + try expect(!math.isPositiveInf(ldexp(math.floatTrueMin(f16), 15 + 24))); + try expect(math.isPositiveInf(ldexp(math.floatTrueMin(f16), 15 + 24 + 1))); + try expect(!math.isPositiveInf(ldexp(math.floatTrueMin(f32), 127 + 149))); + try expect(math.isPositiveInf(ldexp(math.floatTrueMin(f32), 127 + 149 + 1))); + try expect(!math.isPositiveInf(ldexp(math.floatTrueMin(f64), 1023 + 1074))); + try expect(math.isPositiveInf(ldexp(math.floatTrueMin(f64), 1023 + 1074 + 1))); + try expect(!math.isPositiveInf(ldexp(math.floatTrueMin(f128), 16383 + 16494))); + try expect(math.isPositiveInf(ldexp(math.floatTrueMin(f128), 16383 + 16494 + 1))); } diff --git a/lib/std/math/round.zig b/lib/std/math/round.zig index c948431a35..be33a9cfbd 100644 --- a/lib/std/math/round.zig +++ b/lib/std/math/round.zig @@ -29,6 +29,8 @@ pub fn round(x: anytype) @TypeOf(x) { } fn round32(x_: f32) f32 { + const f32_toint = 1.0 / math.floatEps(f32); + var x = x_; const u = @bitCast(u32, x); const e = (u >> 23) & 0xFF; @@ -41,11 +43,11 @@ fn round32(x_: f32) f32 { x = -x; } if (e < 0x7F - 1) { - math.doNotOptimizeAway(x + math.f32_toint); + math.doNotOptimizeAway(x + f32_toint); return 0 * @bitCast(f32, u); } - y = x + math.f32_toint - math.f32_toint - x; + y = x + f32_toint - f32_toint - x; if (y > 0.5) { y = y + x - 1; } else if (y <= -0.5) { @@ -62,6 +64,8 @@ fn round32(x_: f32) f32 { } fn round64(x_: f64) f64 { + const f64_toint = 1.0 / math.floatEps(f64); + var x = x_; const u = @bitCast(u64, x); const e = (u >> 52) & 0x7FF; @@ -74,11 +78,11 @@ fn round64(x_: f64) f64 { x = -x; } if (e < 0x3ff - 1) { - math.doNotOptimizeAway(x + math.f64_toint); + math.doNotOptimizeAway(x + f64_toint); return 0 * @bitCast(f64, u); } - y = x + math.f64_toint - math.f64_toint - x; + y = x + f64_toint - f64_toint - x; if (y > 0.5) { y = y + x - 1; } else if (y <= -0.5) { @@ -95,6 +99,8 @@ fn round64(x_: f64) f64 { } fn round128(x_: f128) f128 { + const f128_toint = 1.0 / math.floatEps(f128); + var x = x_; const u = @bitCast(u128, x); const e = (u >> 112) & 0x7FFF; @@ -107,11 +113,11 @@ fn round128(x_: f128) f128 { x = -x; } if (e < 0x3FFF - 1) { - math.doNotOptimizeAway(x + math.f64_toint); + math.doNotOptimizeAway(x + f128_toint); return 0 * @bitCast(f128, u); } - y = x + math.f128_toint - math.f128_toint - x; + y = x + f128_toint - f128_toint - x; if (y > 0.5) { y = y + x - 1; } else if (y <= -0.5) { |
