aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/complex
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2024-07-31 19:19:27 -0700
committerGitHub <noreply@github.com>2024-07-31 19:19:27 -0700
commit059856acfc9f87d723a90af6a4214e128b8cae2e (patch)
tree851538dd757a467513f7061db4d2ec9b94a2c628 /lib/std/math/complex
parent7c5ee3efde6d948205c6f6eaa7ab52bda3715fea (diff)
parent843885512dd69e083ec9163e6f57822487b46639 (diff)
downloadzig-059856acfc9f87d723a90af6a4214e128b8cae2e.tar.gz
zig-059856acfc9f87d723a90af6a4214e128b8cae2e.zip
Merge pull request #20878 from tiehuis/std-math-complex-fixes
std.math.complex fixes
Diffstat (limited to 'lib/std/math/complex')
-rw-r--r--lib/std/math/complex/abs.zig5
-rw-r--r--lib/std/math/complex/acos.zig7
-rw-r--r--lib/std/math/complex/acosh.zig13
-rw-r--r--lib/std/math/complex/arg.zig5
-rw-r--r--lib/std/math/complex/asin.zig7
-rw-r--r--lib/std/math/complex/asinh.zig7
-rw-r--r--lib/std/math/complex/atan.zig50
-rw-r--r--lib/std/math/complex/atanh.zig7
-rw-r--r--lib/std/math/complex/conj.zig3
-rw-r--r--lib/std/math/complex/cos.zig7
-rw-r--r--lib/std/math/complex/cosh.zig29
-rw-r--r--lib/std/math/complex/log.zig7
-rw-r--r--lib/std/math/complex/pow.zig7
-rw-r--r--lib/std/math/complex/proj.zig3
-rw-r--r--lib/std/math/complex/sin.zig7
-rw-r--r--lib/std/math/complex/sinh.zig12
-rw-r--r--lib/std/math/complex/sqrt.zig16
-rw-r--r--lib/std/math/complex/tan.zig7
-rw-r--r--lib/std/math/complex/tanh.zig23
19 files changed, 102 insertions, 120 deletions
diff --git a/lib/std/math/complex/abs.zig b/lib/std/math/complex/abs.zig
index ab85f2c36c..8b75db4a61 100644
--- a/lib/std/math/complex/abs.zig
+++ b/lib/std/math/complex/abs.zig
@@ -9,10 +9,9 @@ pub fn abs(z: anytype) @TypeOf(z.re, z.im) {
return math.hypot(z.re, z.im);
}
-const epsilon = 0.0001;
-
test abs {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = abs(a);
- try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));
+ try testing.expectApproxEqAbs(5.8309517, c, epsilon);
}
diff --git a/lib/std/math/complex/acos.zig b/lib/std/math/complex/acos.zig
index 9cbfaffe4b..afaae740d9 100644
--- a/lib/std/math/complex/acos.zig
+++ b/lib/std/math/complex/acos.zig
@@ -11,12 +11,11 @@ pub fn acos(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(@as(T, math.pi) / 2 - q.re, -q.im);
}
-const epsilon = 0.0001;
-
test acos {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = acos(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 0.546975, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, -2.452914, epsilon));
+ try testing.expectApproxEqAbs(0.5469737, c.re, epsilon);
+ try testing.expectApproxEqAbs(-2.4529128, c.im, epsilon);
}
diff --git a/lib/std/math/complex/acosh.zig b/lib/std/math/complex/acosh.zig
index dba8b03794..dbb7104343 100644
--- a/lib/std/math/complex/acosh.zig
+++ b/lib/std/math/complex/acosh.zig
@@ -8,15 +8,18 @@ const Complex = cmath.Complex;
pub fn acosh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);
const q = cmath.acos(z);
- return Complex(T).init(-q.im, q.re);
-}
-const epsilon = 0.0001;
+ return if (math.signbit(z.im))
+ Complex(T).init(q.im, -q.re)
+ else
+ Complex(T).init(-q.im, q.re);
+}
test acosh {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = acosh(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 2.452914, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 0.546975, epsilon));
+ try testing.expectApproxEqAbs(2.4529128, c.re, epsilon);
+ try testing.expectApproxEqAbs(0.5469737, c.im, epsilon);
}
diff --git a/lib/std/math/complex/arg.zig b/lib/std/math/complex/arg.zig
index ac69276d96..12986c7c8d 100644
--- a/lib/std/math/complex/arg.zig
+++ b/lib/std/math/complex/arg.zig
@@ -9,10 +9,9 @@ pub fn arg(z: anytype) @TypeOf(z.re, z.im) {
return math.atan2(z.im, z.re);
}
-const epsilon = 0.0001;
-
test arg {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = arg(a);
- try testing.expect(math.approxEqAbs(f32, c, 0.540420, epsilon));
+ try testing.expectApproxEqAbs(0.5404195, c, epsilon);
}
diff --git a/lib/std/math/complex/asin.zig b/lib/std/math/complex/asin.zig
index deacfa26ea..bf3e9a00da 100644
--- a/lib/std/math/complex/asin.zig
+++ b/lib/std/math/complex/asin.zig
@@ -17,12 +17,11 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(r.im, -r.re);
}
-const epsilon = 0.0001;
-
test asin {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = asin(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 1.023822, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 2.452914, epsilon));
+ try testing.expectApproxEqAbs(1.0238227, c.re, epsilon);
+ try testing.expectApproxEqAbs(2.4529128, c.im, epsilon);
}
diff --git a/lib/std/math/complex/asinh.zig b/lib/std/math/complex/asinh.zig
index 2dcfc9c2ac..4923caff50 100644
--- a/lib/std/math/complex/asinh.zig
+++ b/lib/std/math/complex/asinh.zig
@@ -12,12 +12,11 @@ pub fn asinh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(r.im, -r.re);
}
-const epsilon = 0.0001;
-
test asinh {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = asinh(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 2.459831, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 0.533999, epsilon));
+ try testing.expectApproxEqAbs(2.4598298, c.re, epsilon);
+ try testing.expectApproxEqAbs(0.5339993, c.im, epsilon);
}
diff --git a/lib/std/math/complex/atan.zig b/lib/std/math/complex/atan.zig
index 33109178f8..9e9e94622c 100644
--- a/lib/std/math/complex/atan.zig
+++ b/lib/std/math/complex/atan.zig
@@ -32,37 +32,22 @@ fn redupif32(x: f32) f32 {
t -= 0.5;
}
- const u = @as(f32, @floatFromInt(@as(i32, @intFromFloat(t))));
- return ((x - u * DP1) - u * DP2) - t * DP3;
+ const u: f32 = @trunc(t);
+ return ((x - u * DP1) - u * DP2) - u * DP3;
}
fn atan32(z: Complex(f32)) Complex(f32) {
- const maxnum = 1.0e38;
-
const x = z.re;
const y = z.im;
- if ((x == 0.0) and (y > 1.0)) {
- // overflow
- return Complex(f32).init(maxnum, maxnum);
- }
-
const x2 = x * x;
var a = 1.0 - x2 - (y * y);
- if (a == 0.0) {
- // overflow
- return Complex(f32).init(maxnum, maxnum);
- }
var t = 0.5 * math.atan2(2.0 * x, a);
const w = redupif32(t);
t = y - 1.0;
a = x2 + t * t;
- if (a == 0.0) {
- // overflow
- return Complex(f32).init(maxnum, maxnum);
- }
t = y + 1.0;
a = (x2 + (t * t)) / a;
@@ -81,57 +66,42 @@ fn redupif64(x: f64) f64 {
t -= 0.5;
}
- const u = @as(f64, @floatFromInt(@as(i64, @intFromFloat(t))));
- return ((x - u * DP1) - u * DP2) - t * DP3;
+ const u: f64 = @trunc(t);
+ return ((x - u * DP1) - u * DP2) - u * DP3;
}
fn atan64(z: Complex(f64)) Complex(f64) {
- const maxnum = 1.0e308;
-
const x = z.re;
const y = z.im;
- if ((x == 0.0) and (y > 1.0)) {
- // overflow
- return Complex(f64).init(maxnum, maxnum);
- }
-
const x2 = x * x;
var a = 1.0 - x2 - (y * y);
- if (a == 0.0) {
- // overflow
- return Complex(f64).init(maxnum, maxnum);
- }
var t = 0.5 * math.atan2(2.0 * x, a);
const w = redupif64(t);
t = y - 1.0;
a = x2 + t * t;
- if (a == 0.0) {
- // overflow
- return Complex(f64).init(maxnum, maxnum);
- }
t = y + 1.0;
a = (x2 + (t * t)) / a;
return Complex(f64).init(w, 0.25 * @log(a));
}
-const epsilon = 0.0001;
-
test atan32 {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = atan(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 0.086569, epsilon));
+ try testing.expectApproxEqAbs(1.423679, c.re, epsilon);
+ try testing.expectApproxEqAbs(0.086569, c.im, epsilon);
}
test atan64 {
+ const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = atan(a);
- try testing.expect(math.approxEqAbs(f64, c.re, 1.423679, epsilon));
- try testing.expect(math.approxEqAbs(f64, c.im, 0.086569, epsilon));
+ try testing.expectApproxEqAbs(1.4236790442393028, c.re, epsilon);
+ try testing.expectApproxEqAbs(0.08656905917945844, c.im, epsilon);
}
diff --git a/lib/std/math/complex/atanh.zig b/lib/std/math/complex/atanh.zig
index 54d21fc433..86d5b3df23 100644
--- a/lib/std/math/complex/atanh.zig
+++ b/lib/std/math/complex/atanh.zig
@@ -12,12 +12,11 @@ pub fn atanh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(r.im, -r.re);
}
-const epsilon = 0.0001;
-
test atanh {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = atanh(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 0.146947, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 1.480870, epsilon));
+ try testing.expectApproxEqAbs(0.14694665, c.re, epsilon);
+ try testing.expectApproxEqAbs(1.4808695, c.im, epsilon);
}
diff --git a/lib/std/math/complex/conj.zig b/lib/std/math/complex/conj.zig
index b5a4d063d3..4a271c9801 100644
--- a/lib/std/math/complex/conj.zig
+++ b/lib/std/math/complex/conj.zig
@@ -14,5 +14,6 @@ test conj {
const a = Complex(f32).init(5, 3);
const c = a.conjugate();
- try testing.expect(c.re == 5 and c.im == -3);
+ try testing.expectEqual(5, c.re);
+ try testing.expectEqual(-3, c.im);
}
diff --git a/lib/std/math/complex/cos.zig b/lib/std/math/complex/cos.zig
index 389dd013e3..fee1a8c37a 100644
--- a/lib/std/math/complex/cos.zig
+++ b/lib/std/math/complex/cos.zig
@@ -11,12 +11,11 @@ pub fn cos(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return cmath.cosh(p);
}
-const epsilon = 0.0001;
-
test cos {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = cos(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 2.855815, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 9.606383, epsilon));
+ try testing.expectApproxEqAbs(2.8558152, c.re, epsilon);
+ try testing.expectApproxEqAbs(9.606383, c.im, epsilon);
}
diff --git a/lib/std/math/complex/cosh.zig b/lib/std/math/complex/cosh.zig
index 42f5b432ec..d41b3fb06a 100644
--- a/lib/std/math/complex/cosh.zig
+++ b/lib/std/math/complex/cosh.zig
@@ -34,7 +34,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
if (ix < 0x7f800000 and iy < 0x7f800000) {
if (iy == 0) {
- return Complex(f32).init(math.cosh(x), y);
+ return Complex(f32).init(math.cosh(x), x * y);
}
// small x: normal case
if (ix < 0x41100000) {
@@ -45,7 +45,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
if (ix < 0x42b17218) {
// x < 88.7: exp(|x|) won't overflow
const h = @exp(@abs(x)) * 0.5;
- return Complex(f32).init(math.copysign(h, x) * @cos(y), h * @sin(y));
+ return Complex(f32).init(h * @cos(y), math.copysign(h, x) * @sin(y));
}
// x < 192.7: scale to avoid overflow
else if (ix < 0x4340b1e7) {
@@ -68,7 +68,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
if (hx & 0x7fffff == 0) {
return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), x) * y);
}
- return Complex(f32).init(x, math.copysign(@as(f32, 0.0), (x + x) * y));
+ return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), (x + x) * y));
}
if (ix < 0x7f800000 and iy >= 0x7f800000) {
@@ -123,7 +123,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
}
// x >= 1455: result always overflows
else {
- const h = 0x1p1023;
+ const h = 0x1p1023 * x;
return Complex(f64).init(h * h * @cos(y), h * @sin(y));
}
}
@@ -153,20 +153,29 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
}
-const epsilon = 0.0001;
-
test cosh32 {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = cosh(a);
- try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 10.471557, epsilon));
+ try testing.expectApproxEqAbs(-73.467300, c.re, epsilon);
+ try testing.expectApproxEqAbs(10.471557, c.im, epsilon);
}
test cosh64 {
+ const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = cosh(a);
- try testing.expect(math.approxEqAbs(f64, c.re, -73.467300, epsilon));
- try testing.expect(math.approxEqAbs(f64, c.im, 10.471557, epsilon));
+ try testing.expectApproxEqAbs(-73.46729221264526, c.re, epsilon);
+ try testing.expectApproxEqAbs(10.471557674805572, c.im, epsilon);
+}
+
+test "cosh64 musl" {
+ const epsilon = math.floatEps(f64);
+ const a = Complex(f64).init(7.44648873421389e17, 1.6008058402057622e19);
+ const c = cosh(a);
+
+ try testing.expectApproxEqAbs(std.math.inf(f64), c.re, epsilon);
+ try testing.expectApproxEqAbs(std.math.inf(f64), c.im, epsilon);
}
diff --git a/lib/std/math/complex/log.zig b/lib/std/math/complex/log.zig
index 65859c2dce..39174431f1 100644
--- a/lib/std/math/complex/log.zig
+++ b/lib/std/math/complex/log.zig
@@ -13,12 +13,11 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(@log(r), phi);
}
-const epsilon = 0.0001;
-
test log {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = log(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 1.763180, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 0.540419, epsilon));
+ try testing.expectApproxEqAbs(1.7631803, c.re, epsilon);
+ try testing.expectApproxEqAbs(0.5404195, c.im, epsilon);
}
diff --git a/lib/std/math/complex/pow.zig b/lib/std/math/complex/pow.zig
index 874e4037dc..6fcca6b864 100644
--- a/lib/std/math/complex/pow.zig
+++ b/lib/std/math/complex/pow.zig
@@ -9,13 +9,12 @@ pub fn pow(z: anytype, s: anytype) Complex(@TypeOf(z.re, z.im, s.re, s.im)) {
return cmath.exp(cmath.log(z).mul(s));
}
-const epsilon = 0.0001;
-
test pow {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const b = Complex(f32).init(2.3, -1.3);
const c = pow(a, b);
- try testing.expect(math.approxEqAbs(f32, c.re, 58.049110, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, -101.003433, epsilon));
+ try testing.expectApproxEqAbs(58.049110, c.re, epsilon);
+ try testing.expectApproxEqAbs(-101.003433, c.im, epsilon);
}
diff --git a/lib/std/math/complex/proj.zig b/lib/std/math/complex/proj.zig
index b7d3d58b12..243e5133c3 100644
--- a/lib/std/math/complex/proj.zig
+++ b/lib/std/math/complex/proj.zig
@@ -19,5 +19,6 @@ test proj {
const a = Complex(f32).init(5, 3);
const c = proj(a);
- try testing.expect(c.re == 5 and c.im == 3);
+ try testing.expectEqual(5, c.re);
+ try testing.expectEqual(3, c.im);
}
diff --git a/lib/std/math/complex/sin.zig b/lib/std/math/complex/sin.zig
index 3ca2419e43..5ce5b335f8 100644
--- a/lib/std/math/complex/sin.zig
+++ b/lib/std/math/complex/sin.zig
@@ -12,12 +12,11 @@ pub fn sin(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(q.im, -q.re);
}
-const epsilon = 0.0001;
-
test sin {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = sin(a);
- try testing.expect(math.approxEqAbs(f32, c.re, -9.654126, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 2.841692, epsilon));
+ try testing.expectApproxEqAbs(-9.654126, c.re, epsilon);
+ try testing.expectApproxEqAbs(2.8416924, c.im, epsilon);
}
diff --git a/lib/std/math/complex/sinh.zig b/lib/std/math/complex/sinh.zig
index 911587a9c5..ae8fadb9ab 100644
--- a/lib/std/math/complex/sinh.zig
+++ b/lib/std/math/complex/sinh.zig
@@ -152,20 +152,20 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
}
-const epsilon = 0.0001;
-
test sinh32 {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = sinh(a);
- try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 10.472508, epsilon));
+ try testing.expectApproxEqAbs(-73.460617, c.re, epsilon);
+ try testing.expectApproxEqAbs(10.472508, c.im, epsilon);
}
test sinh64 {
+ const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = sinh(a);
- try testing.expect(math.approxEqAbs(f64, c.re, -73.460617, epsilon));
- try testing.expect(math.approxEqAbs(f64, c.im, 10.472508, epsilon));
+ try testing.expectApproxEqAbs(-73.46062169567367, c.re, epsilon);
+ try testing.expectApproxEqAbs(10.472508533940392, c.im, epsilon);
}
diff --git a/lib/std/math/complex/sqrt.zig b/lib/std/math/complex/sqrt.zig
index 0edf18fb23..f8f7798ae9 100644
--- a/lib/std/math/complex/sqrt.zig
+++ b/lib/std/math/complex/sqrt.zig
@@ -43,7 +43,7 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
// sqrt(-inf + i nan) = nan +- inf i
// sqrt(-inf + iy) = 0 + inf i
if (math.signbit(x)) {
- return Complex(f32).init(@abs(x - y), math.copysign(x, y));
+ return Complex(f32).init(@abs(y - y), math.copysign(x, y));
} else {
return Complex(f32).init(x, math.copysign(y - y, y));
}
@@ -94,7 +94,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
// sqrt(-inf + i nan) = nan +- inf i
// sqrt(-inf + iy) = 0 + inf i
if (math.signbit(x)) {
- return Complex(f64).init(@abs(x - y), math.copysign(x, y));
+ return Complex(f64).init(@abs(y - y), math.copysign(x, y));
} else {
return Complex(f64).init(x, math.copysign(y - y, y));
}
@@ -127,20 +127,20 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
return result;
}
-const epsilon = 0.0001;
-
test sqrt32 {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = sqrt(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 0.644574, epsilon));
+ try testing.expectApproxEqAbs(2.3271174, c.re, epsilon);
+ try testing.expectApproxEqAbs(0.6445742, c.im, epsilon);
}
test sqrt64 {
+ const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = sqrt(a);
- try testing.expect(math.approxEqAbs(f64, c.re, 2.3271175190399496, epsilon));
- try testing.expect(math.approxEqAbs(f64, c.im, 0.6445742373246469, epsilon));
+ try testing.expectApproxEqAbs(2.3271175190399496, c.re, epsilon);
+ try testing.expectApproxEqAbs(0.6445742373246469, c.im, epsilon);
}
diff --git a/lib/std/math/complex/tan.zig b/lib/std/math/complex/tan.zig
index ad5b1b47b6..4c3629046a 100644
--- a/lib/std/math/complex/tan.zig
+++ b/lib/std/math/complex/tan.zig
@@ -12,12 +12,11 @@ pub fn tan(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(r.im, -r.re);
}
-const epsilon = 0.0001;
-
test tan {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = tan(a);
- try testing.expect(math.approxEqAbs(f32, c.re, -0.002708233, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, 1.004165, epsilon));
+ try testing.expectApproxEqAbs(-0.002708233, c.re, epsilon);
+ try testing.expectApproxEqAbs(1.0041647, c.im, epsilon);
}
diff --git a/lib/std/math/complex/tanh.zig b/lib/std/math/complex/tanh.zig
index 8a1d46eca7..af2ed4f061 100644
--- a/lib/std/math/complex/tanh.zig
+++ b/lib/std/math/complex/tanh.zig
@@ -70,7 +70,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
const ix = hx & 0x7fffffff;
if (ix >= 0x7ff00000) {
- if ((ix & 0x7fffff) | lx != 0) {
+ if ((ix & 0xfffff) | lx != 0) {
const r = if (y == 0) y else x * y;
return Complex(f64).init(x, r);
}
@@ -101,20 +101,29 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
return Complex(f64).init((beta * rho * s) / den, t / den);
}
-const epsilon = 0.0001;
-
test tanh32 {
+ const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = tanh(a);
- try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon));
- try testing.expect(math.approxEqAbs(f32, c.im, -0.000025, epsilon));
+ try testing.expectApproxEqAbs(0.99991274, c.re, epsilon);
+ try testing.expectApproxEqAbs(-0.00002536878, c.im, epsilon);
}
test tanh64 {
+ const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = tanh(a);
- try testing.expect(math.approxEqAbs(f64, c.re, 0.999913, epsilon));
- try testing.expect(math.approxEqAbs(f64, c.im, -0.000025, epsilon));
+ try testing.expectApproxEqAbs(0.9999128201513536, c.re, epsilon);
+ try testing.expectApproxEqAbs(-0.00002536867620767604, c.im, epsilon);
+}
+
+test "tanh64 musl" {
+ const epsilon = math.floatEps(f64);
+ const a = Complex(f64).init(std.math.inf(f64), std.math.inf(f64));
+ const c = tanh(a);
+
+ try testing.expectApproxEqAbs(1, c.re, epsilon);
+ try testing.expectApproxEqAbs(0, c.im, epsilon);
}