From 0fda2f31aae2109886c74b352ea4dee23ce6eb74 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Tue, 30 Jul 2024 16:29:51 +1200 Subject: [PATCH] std.math.complex: tighten existing test bounds --- lib/std/math/complex/abs.zig | 5 ++--- lib/std/math/complex/acos.zig | 7 +++---- lib/std/math/complex/acosh.zig | 7 +++---- lib/std/math/complex/arg.zig | 5 ++--- lib/std/math/complex/asin.zig | 7 +++---- lib/std/math/complex/asinh.zig | 7 +++---- lib/std/math/complex/atan.zig | 12 ++++++------ lib/std/math/complex/atanh.zig | 7 +++---- lib/std/math/complex/conj.zig | 3 ++- lib/std/math/complex/cos.zig | 7 +++---- lib/std/math/complex/cosh.zig | 12 ++++++------ lib/std/math/complex/log.zig | 7 +++---- lib/std/math/complex/pow.zig | 7 +++---- lib/std/math/complex/proj.zig | 3 ++- lib/std/math/complex/sin.zig | 7 +++---- lib/std/math/complex/sinh.zig | 12 ++++++------ lib/std/math/complex/sqrt.zig | 12 ++++++------ lib/std/math/complex/tan.zig | 7 +++---- lib/std/math/complex/tanh.zig | 12 ++++++------ 19 files changed, 68 insertions(+), 78 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 24224fd93b..dbb7104343 100644 --- a/lib/std/math/complex/acosh.zig +++ b/lib/std/math/complex/acosh.zig @@ -15,12 +15,11 @@ pub fn acosh(z: anytype) Complex(@TypeOf(z.re, z.im)) { Complex(T).init(-q.im, q.re); } -const epsilon = 0.0001; - 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 7269ea9ead..9e9e94622c 100644 --- a/lib/std/math/complex/atan.zig +++ b/lib/std/math/complex/atan.zig @@ -88,20 +88,20 @@ fn atan64(z: Complex(f64)) Complex(f64) { 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 18443da171..c672c71ece 100644 --- a/lib/std/math/complex/cosh.zig +++ b/lib/std/math/complex/cosh.zig @@ -153,20 +153,20 @@ 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); } 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 ef24cc7726..f8f7798ae9 100644 --- a/lib/std/math/complex/sqrt.zig +++ b/lib/std/math/complex/sqrt.zig @@ -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..ad75479322 100644 --- a/lib/std/math/complex/tanh.zig +++ b/lib/std/math/complex/tanh.zig @@ -101,20 +101,20 @@ 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); }