From ff14451b4a328ca6862637a88912000d11aa2bf3 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 2 Nov 2020 13:25:50 +0100 Subject: std: Implement more useful approxEq semantics Comparisons with absolute epsilons are usually useful when comparing numbers to zero, for non-zero numbers it's advised to switch to relative epsilons instead to obtain meaningful results (check [1] for more details). The new API introduces approxEqAbs and approxEqRel, where the former aliases and deprecated the old `approxEq`, allowing the user to pick the right tool for the job. The documentation is meant to guide the user in the choice of the correct alternative. [1] https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ --- lib/std/math/complex.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/std/math/complex.zig') diff --git a/lib/std/math/complex.zig b/lib/std/math/complex.zig index ec76f425eb..f9f13a1161 100644 --- a/lib/std/math/complex.zig +++ b/lib/std/math/complex.zig @@ -138,8 +138,8 @@ test "complex.div" { const b = Complex(f32).new(2, 7); const c = a.div(b); - testing.expect(math.approxEq(f32, c.re, @as(f32, 31) / 53, epsilon) and - math.approxEq(f32, c.im, @as(f32, -29) / 53, epsilon)); + testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 31) / 53, epsilon) and + math.approxEqAbs(f32, c.im, @as(f32, -29) / 53, epsilon)); } test "complex.conjugate" { @@ -153,15 +153,15 @@ test "complex.reciprocal" { const a = Complex(f32).new(5, 3); const c = a.reciprocal(); - testing.expect(math.approxEq(f32, c.re, @as(f32, 5) / 34, epsilon) and - math.approxEq(f32, c.im, @as(f32, -3) / 34, epsilon)); + testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 5) / 34, epsilon) and + math.approxEqAbs(f32, c.im, @as(f32, -3) / 34, epsilon)); } test "complex.magnitude" { const a = Complex(f32).new(5, 3); const c = a.magnitude(); - testing.expect(math.approxEq(f32, c, 5.83095, epsilon)); + testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon)); } test "complex.cmath" { -- cgit v1.2.3