| 1 | const std = @import("../../std.zig"); |
| 2 | const testing = std.testing; |
| 3 | const math = std.math; |
| 4 | const cmath = math.complex; |
| 5 | const Complex = cmath.Complex; |
| 6 | |
| 7 | /// Returns the absolute value (modulus) of z. |
| 8 | pub fn abs(z: anytype) @TypeOf(z.re, z.im) { |
| 9 | return math.hypot(z.re, z.im); |
| 10 | } |
| 11 | |
| 12 | test abs { |
| 13 | const epsilon = math.floatEps(f32); |
| 14 | const a = Complex(f32).init(5, 3); |
| 15 | const c = abs(a); |
| 16 | try testing.expectApproxEqAbs(5.8309517, c, epsilon); |
| 17 | } |