Currently, the Complex.hypot uses a simple call to @sqrt, which is ok (and probably better?) for small numbers:
|
return @sqrt(self.re * self.re + self.im * self.im); |
But there is already a dedicated function for calculating an absolute of a complex number, that references
math.hypot:
|
/// Returns the absolute value (modulus) of z. |
|
pub fn abs(z: anytype) @TypeOf(z.re, z.im) { |
|
return math.hypot(z.re, z.im); |
|
} |
Which in turn states that it avoids unnecessary overflow and underflow:
|
/// Returns sqrt(x * x + y * y), avoiding unnecessary overflow and underflow. |
|
/// |
|
/// Special Cases: |
|
/// |
|
/// | x | y | hypot | |
|
/// |-------|-------|-------| |
|
/// | +-inf | any | +inf | |
|
/// | any | +-inf | +inf | |
|
/// | nan | fin | nan | |
|
/// | fin | nan | nan | |
|
pub fn hypot(x: anytype, y: anytype) @TypeOf(x, y) { |
Currently, the
Complex.hypotuses a simple call to@sqrt, which is ok (and probably better?) for small numbers:zig/lib/std/math/complex.zig
Line 116 in d590b87
math.hypot:zig/lib/std/math/complex/abs.zig
Lines 7 to 10 in d590b87
zig/lib/std/math/hypot.zig
Lines 13 to 23 in d590b87