From a8a4f1755e440bc6ff8c27fad0da91ed83f20841 Mon Sep 17 00:00:00 2001 From: Ruben Dimas <57540512+rdimas-ut@users.noreply.github.com> Date: Tue, 12 Sep 2023 02:19:21 -0400 Subject: [PATCH] std.math.asinh: fixed -0.0 evaluating to 0.0 --- lib/std/math/asinh.zig | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/std/math/asinh.zig b/lib/std/math/asinh.zig index 13b1045bf6..c410f82735 100644 --- a/lib/std/math/asinh.zig +++ b/lib/std/math/asinh.zig @@ -28,30 +28,25 @@ pub fn asinh(x: anytype) @TypeOf(x) { fn asinh32(x: f32) f32 { const u = @as(u32, @bitCast(x)); const i = u & 0x7FFFFFFF; - const s = i >> 31; + const s = u >> 31; var rx = @as(f32, @bitCast(i)); // |x| - // TODO: Shouldn't need this explicit check. - if (math.isNegativeInf(x)) { - return x; - } - // |x| >= 0x1p12 or inf or nan if (i >= 0x3F800000 + (12 << 23)) { rx = @log(rx) + 0.69314718055994530941723212145817656; } // |x| >= 2 else if (i >= 0x3F800000 + (1 << 23)) { - rx = @log(2 * x + 1 / (@sqrt(x * x + 1) + x)); + rx = @log(2 * rx + 1 / (@sqrt(rx * rx + 1) + rx)); } // |x| >= 0x1p-12, up to 1.6ulp error else if (i >= 0x3F800000 - (12 << 23)) { - rx = math.log1p(x + x * x / (@sqrt(x * x + 1) + 1)); + rx = math.log1p(rx + rx * rx / (@sqrt(rx * rx + 1) + 1)); } // |x| < 0x1p-12, inexact if x != 0 else { - math.doNotOptimizeAway(x + 0x1.0p120); + math.doNotOptimizeAway(rx + 0x1.0p120); } return if (s != 0) -rx else rx; @@ -60,29 +55,25 @@ fn asinh32(x: f32) f32 { fn asinh64(x: f64) f64 { const u = @as(u64, @bitCast(x)); const e = (u >> 52) & 0x7FF; - const s = e >> 63; + const s = u >> 63; var rx = @as(f64, @bitCast(u & (maxInt(u64) >> 1))); // |x| - if (math.isNegativeInf(x)) { - return x; - } - // |x| >= 0x1p26 or inf or nan if (e >= 0x3FF + 26) { rx = @log(rx) + 0.693147180559945309417232121458176568; } // |x| >= 2 else if (e >= 0x3FF + 1) { - rx = @log(2 * x + 1 / (@sqrt(x * x + 1) + x)); + rx = @log(2 * rx + 1 / (@sqrt(rx * rx + 1) + rx)); } // |x| >= 0x1p-12, up to 1.6ulp error else if (e >= 0x3FF - 26) { - rx = math.log1p(x + x * x / (@sqrt(x * x + 1) + 1)); + rx = math.log1p(rx + rx * rx / (@sqrt(rx * rx + 1) + 1)); } // |x| < 0x1p-12, inexact if x != 0 else { - math.doNotOptimizeAway(x + 0x1.0p120); + math.doNotOptimizeAway(rx + 0x1.0p120); } return if (s != 0) -rx else rx; @@ -121,7 +112,7 @@ test "math.asinh64" { test "math.asinh32.special" { try expect(asinh32(0.0) == 0.0); - try expect(asinh32(-0.0) == -0.0); + try expect(@as(u32, @bitCast(asinh32(-0.0))) == @as(u32, 2147483648)); try expect(math.isPositiveInf(asinh32(math.inf(f32)))); try expect(math.isNegativeInf(asinh32(-math.inf(f32)))); try expect(math.isNan(asinh32(math.nan(f32)))); @@ -129,7 +120,7 @@ test "math.asinh32.special" { test "math.asinh64.special" { try expect(asinh64(0.0) == 0.0); - try expect(asinh64(-0.0) == -0.0); + try expect(@as(u64, @bitCast(asinh64(-0.0))) == @as(u64, 9223372036854775808)); try expect(math.isPositiveInf(asinh64(math.inf(f64)))); try expect(math.isNegativeInf(asinh64(-math.inf(f64)))); try expect(math.isNan(asinh64(math.nan(f64))));