From e70c543bc4c097d7c3287feb0f233a13d4dc3829 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Thu, 13 Sep 2018 20:33:05 +1200 Subject: [PATCH] math/complex: cexp test correction and ldexp usage fix --- std/math/complex/cosh.zig | 4 ++-- std/math/complex/exp.zig | 9 ++++----- std/math/complex/sinh.zig | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/std/math/complex/cosh.zig b/std/math/complex/cosh.zig index a2e31631ea..000c4ea581 100644 --- a/std/math/complex/cosh.zig +++ b/std/math/complex/cosh.zig @@ -44,7 +44,7 @@ fn cosh32(z: *const Complex(f32)) Complex(f32) { else if (ix < 0x4340b1e7) { const v = Complex(f32).new(math.fabs(x), y); const r = ldexp_cexp(v, -1); - return Complex(f32).new(x, y * math.copysign(f32, 1, x)); + return Complex(f32).new(r.re, r.im * math.copysign(f32, 1, x)); } // x >= 192.7: result always overflows else { @@ -112,7 +112,7 @@ fn cosh64(z: *const Complex(f64)) Complex(f64) { else if (ix < 0x4096bbaa) { const v = Complex(f64).new(math.fabs(x), y); const r = ldexp_cexp(v, -1); - return Complex(f64).new(x, y * math.copysign(f64, 1, x)); + return Complex(f64).new(r.re, r.im * math.copysign(f64, 1, x)); } // x >= 1455: result always overflows else { diff --git a/std/math/complex/exp.zig b/std/math/complex/exp.zig index e696ee42b4..0473f653b6 100644 --- a/std/math/complex/exp.zig +++ b/std/math/complex/exp.zig @@ -69,7 +69,7 @@ fn exp64(z: Complex(f64)) Complex(f64) { const y = z.im; const fy = @bitCast(u64, y); - const hy = u32(fy >> 32) & 0x7fffffff; + const hy = @intCast(u32, (fy >> 32) & 0x7fffffff); const ly = @truncate(u32, fy); // cexp(x + i0) = exp(x) + i0 @@ -78,7 +78,7 @@ fn exp64(z: Complex(f64)) Complex(f64) { } const fx = @bitCast(u64, x); - const hx = u32(fx >> 32); + const hx = @intCast(u32, fx >> 32); const lx = @truncate(u32, fx); // cexp(0 + iy) = cos(y) + isin(y) @@ -101,8 +101,7 @@ fn exp64(z: Complex(f64)) Complex(f64) { // 709.7 <= x <= 1454.3 so must scale if (hx >= exp_overflow and hx <= cexp_overflow) { - const r = ldexp_cexp(z, 0); - return r.*; + return ldexp_cexp(z, 0); } // - x < exp_overflow => exp(x) won't overflow (common) // - x > cexp_overflow, so exp(x) * s overflows for s > 0 // - x = +-inf @@ -124,7 +123,7 @@ test "complex.cexp32" { } test "complex.cexp64" { - const a = Complex(f32).new(5, 3); + const a = Complex(f64).new(5, 3); const c = exp(a); debug.assert(math.approxEq(f64, c.re, -146.927917, epsilon)); diff --git a/std/math/complex/sinh.zig b/std/math/complex/sinh.zig index ab23c5c74d..dc19a0ba1b 100644 --- a/std/math/complex/sinh.zig +++ b/std/math/complex/sinh.zig @@ -44,7 +44,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) { else if (ix < 0x4340b1e7) { const v = Complex(f32).new(math.fabs(x), y); const r = ldexp_cexp(v, -1); - return Complex(f32).new(x * math.copysign(f32, 1, x), y); + return Complex(f32).new(r.re * math.copysign(f32, 1, x), r.im); } // x >= 192.7: result always overflows else { @@ -111,7 +111,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) { else if (ix < 0x4096bbaa) { const v = Complex(f64).new(math.fabs(x), y); const r = ldexp_cexp(v, -1); - return Complex(f64).new(x * math.copysign(f64, 1, x), y); + return Complex(f64).new(r.re * math.copysign(f64, 1, x), r.im); } // x >= 1455: result always overflows else {