Commit e70c543bc4

Marc Tiehuis <marctiehuis@gmail.com>
2018-09-13 10:33:05
math/complex: cexp test correction and ldexp usage fix
1 parent afe6316
Changed files (3)
std
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 {
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));
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 {