diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-07-31 10:56:49 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2025-07-31 10:57:04 +0100 |
| commit | 64bf8bb146099b51d74635a1f116a913e442bcf4 (patch) | |
| tree | 2f9cba090e0fdea54b9bffdbd76e5c729756464d /lib/std/math | |
| parent | e664bf4d81e9266ee4749b5da88cab4554499bf6 (diff) | |
| download | zig-64bf8bb146099b51d74635a1f116a913e442bcf4.tar.gz zig-64bf8bb146099b51d74635a1f116a913e442bcf4.zip | |
std: stop relying on precision-losing coercions
Diffstat (limited to 'lib/std/math')
| -rw-r--r-- | lib/std/math/gamma.zig | 32 | ||||
| -rw-r--r-- | lib/std/math/modf.zig | 2 | ||||
| -rw-r--r-- | lib/std/math/pow.zig | 4 |
3 files changed, 19 insertions, 19 deletions
diff --git a/lib/std/math/gamma.zig b/lib/std/math/gamma.zig index 5577f71461..ce9a2b07f9 100644 --- a/lib/std/math/gamma.zig +++ b/lib/std/math/gamma.zig @@ -189,19 +189,19 @@ fn series(comptime T: type, abs: T) T { 2.5066282746310002701649081771338373386264310793408, }; const denominator = [_]T{ - 0, - 39916800, - 120543840, - 150917976, - 105258076, - 45995730, - 13339535, - 2637558, - 357423, - 32670, - 1925, - 66, - 1, + 0.0, + 39916800.0, + 120543840.0, + 150917976.0, + 105258076.0, + 45995730.0, + 13339535.0, + 2637558.0, + 357423.0, + 32670.0, + 1925.0, + 66.0, + 1.0, }; var num: T = 0; var den: T = 0; @@ -244,9 +244,9 @@ const expectApproxEqRel = std.testing.expectApproxEqRel; test gamma { inline for (&.{ f32, f64 }) |T| { const eps = @sqrt(std.math.floatEps(T)); - try expectApproxEqRel(@as(T, 120), gamma(T, 6), eps); - try expectApproxEqRel(@as(T, 362880), gamma(T, 10), eps); - try expectApproxEqRel(@as(T, 6402373705728000), gamma(T, 19), eps); + try expectApproxEqRel(@as(T, 120.0), gamma(T, 6), eps); + try expectApproxEqRel(@as(T, 362880.0), gamma(T, 10), eps); + try expectApproxEqRel(@as(T, 6402373705728000.0), gamma(T, 19), eps); try expectApproxEqRel(@as(T, 332.7590766955334570), gamma(T, 0.003), eps); try expectApproxEqRel(@as(T, 1.377260301981044573), gamma(T, 0.654), eps); diff --git a/lib/std/math/modf.zig b/lib/std/math/modf.zig index 77d58bd34e..dda34454e3 100644 --- a/lib/std/math/modf.zig +++ b/lib/std/math/modf.zig @@ -74,7 +74,7 @@ fn ModfTests(comptime T: type) type { r = modf(@as(T, 43874.3)); try expectEqual(43874.0, r.ipart); // account for precision error - const expected_b: T = 43874.3 - @as(T, 43874); + const expected_b: T = 43874.3 - @as(T, 43874.0); try expectApproxEqAbs(expected_b, r.fpart, epsilon); r = modf(@as(T, 1234.340780)); diff --git a/lib/std/math/pow.zig b/lib/std/math/pow.zig index acaafe7609..42f28ce465 100644 --- a/lib/std/math/pow.zig +++ b/lib/std/math/pow.zig @@ -192,8 +192,8 @@ fn isOddInteger(x: f64) bool { } test isOddInteger { - try expect(isOddInteger(math.maxInt(i64) * 2) == false); - try expect(isOddInteger(math.maxInt(i64) * 2 + 1) == false); + try expect(isOddInteger(@floatFromInt(math.maxInt(i64) * 2)) == false); + try expect(isOddInteger(@floatFromInt(math.maxInt(i64) * 2 + 1)) == false); try expect(isOddInteger(1 << 53) == false); try expect(isOddInteger(12.0) == false); try expect(isOddInteger(15.0) == true); |
