aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/complex/abs.zig
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/math/complex/abs.zig')
-rw-r--r--lib/std/math/complex/abs.zig19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/std/math/complex/abs.zig b/lib/std/math/complex/abs.zig
new file mode 100644
index 0000000000..8105f57218
--- /dev/null
+++ b/lib/std/math/complex/abs.zig
@@ -0,0 +1,19 @@
+const std = @import("../../std.zig");
+const testing = std.testing;
+const math = std.math;
+const cmath = math.complex;
+const Complex = cmath.Complex;
+
+/// Returns the absolute value (modulus) of z.
+pub fn abs(z: var) @typeOf(z.re) {
+ const T = @typeOf(z.re);
+ return math.hypot(T, z.re, z.im);
+}
+
+const epsilon = 0.0001;
+
+test "complex.cabs" {
+ const a = Complex(f32).new(5, 3);
+ const c = abs(a);
+ testing.expect(math.approxEq(f32, c, 5.83095, epsilon));
+}