master
 1const std = @import("../../std.zig");
 2const testing = std.testing;
 3const math = std.math;
 4const cmath = math.complex;
 5const Complex = cmath.Complex;
 6
 7/// Returns the natural logarithm of z.
 8pub fn log(z: anytype) Complex(@TypeOf(z.re, z.im)) {
 9    const T = @TypeOf(z.re, z.im);
10    const r = cmath.abs(z);
11    const phi = cmath.arg(z);
12
13    return Complex(T).init(@log(r), phi);
14}
15
16test log {
17    const epsilon = math.floatEps(f32);
18    const a = Complex(f32).init(5, 3);
19    const c = log(a);
20
21    try testing.expectApproxEqAbs(1.7631803, c.re, epsilon);
22    try testing.expectApproxEqAbs(0.5404195, c.im, epsilon);
23}