authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2024-07-30 20:31:22+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2024-07-30 20:31:22+12:00
log843885512dd69e083ec9163e6f57822487b46639
tree3f3cfc016b929904ace3ab925dbf235789b19c1d
parent0fda2f31aae2109886c74b352ea4dee23ce6eb74

std.math.complex: fix cosh/tanh


2 files changed, 20 insertions(+), 2 deletions(-)

lib/std/math/complex/cosh.zig+10-1
...@@ -123,7 +123,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {...@@ -123,7 +123,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
123 }123 }
124 // x >= 1455: result always overflows124 // x >= 1455: result always overflows
125 else {125 else {
126 const h = 0x1p1023;126 const h = 0x1p1023 * x;
127 return Complex(f64).init(h * h * @cos(y), h * @sin(y));127 return Complex(f64).init(h * h * @cos(y), h * @sin(y));
128 }128 }
129 }129 }
...@@ -170,3 +170,12 @@ test cosh64 {...@@ -170,3 +170,12 @@ test cosh64 {
170 try testing.expectApproxEqAbs(-73.46729221264526, c.re, epsilon);170 try testing.expectApproxEqAbs(-73.46729221264526, c.re, epsilon);
171 try testing.expectApproxEqAbs(10.471557674805572, c.im, epsilon);171 try testing.expectApproxEqAbs(10.471557674805572, c.im, epsilon);
172}172}
173
174test "cosh64 musl" {
175 const epsilon = math.floatEps(f64);
176 const a = Complex(f64).init(7.44648873421389e17, 1.6008058402057622e19);
177 const c = cosh(a);
178
179 try testing.expectApproxEqAbs(std.math.inf(f64), c.re, epsilon);
180 try testing.expectApproxEqAbs(std.math.inf(f64), c.im, epsilon);
181}
lib/std/math/complex/tanh.zig+10-1
...@@ -70,7 +70,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {...@@ -70,7 +70,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
70 const ix = hx & 0x7fffffff;70 const ix = hx & 0x7fffffff;
7171
72 if (ix >= 0x7ff00000) {72 if (ix >= 0x7ff00000) {
73 if ((ix & 0x7fffff) | lx != 0) {73 if ((ix & 0xfffff) | lx != 0) {
74 const r = if (y == 0) y else x * y;74 const r = if (y == 0) y else x * y;
75 return Complex(f64).init(x, r);75 return Complex(f64).init(x, r);
76 }76 }
...@@ -118,3 +118,12 @@ test tanh64 {...@@ -118,3 +118,12 @@ test tanh64 {
118 try testing.expectApproxEqAbs(0.9999128201513536, c.re, epsilon);118 try testing.expectApproxEqAbs(0.9999128201513536, c.re, epsilon);
119 try testing.expectApproxEqAbs(-0.00002536867620767604, c.im, epsilon);119 try testing.expectApproxEqAbs(-0.00002536867620767604, c.im, epsilon);
120}120}
121
122test "tanh64 musl" {
123 const epsilon = math.floatEps(f64);
124 const a = Complex(f64).init(std.math.inf(f64), std.math.inf(f64));
125 const c = tanh(a);
126
127 try testing.expectApproxEqAbs(1, c.re, epsilon);
128 try testing.expectApproxEqAbs(0, c.im, epsilon);
129}