Skip to content

Commit 659056a

Browse files
tiehuisIgor Stojkovic
authored and
Igor Stojkovic
committed
std.math.complex: fix cosh/tanh
1 parent c1c1b2c commit 659056a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/std/math/complex/cosh.zig

+10-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
123123
}
124124
// x >= 1455: result always overflows
125125
else {
126-
const h = 0x1p1023;
126+
const h = 0x1p1023 * x;
127127
return Complex(f64).init(h * h * @cos(y), h * @sin(y));
128128
}
129129
}
@@ -170,3 +170,12 @@ test cosh64 {
170170
try testing.expectApproxEqAbs(-73.46729221264526, c.re, epsilon);
171171
try testing.expectApproxEqAbs(10.471557674805572, c.im, epsilon);
172172
}
173+
174+
test "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
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
7070
const ix = hx & 0x7fffffff;
7171

7272
if (ix >= 0x7ff00000) {
73-
if ((ix & 0x7fffff) | lx != 0) {
73+
if ((ix & 0xfffff) | lx != 0) {
7474
const r = if (y == 0) y else x * y;
7575
return Complex(f64).init(x, r);
7676
}
@@ -118,3 +118,12 @@ test tanh64 {
118118
try testing.expectApproxEqAbs(0.9999128201513536, c.re, epsilon);
119119
try testing.expectApproxEqAbs(-0.00002536867620767604, c.im, epsilon);
120120
}
121+
122+
test "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+
}

0 commit comments

Comments
 (0)