authorgravatar for 48591413+chrboesch@users.noreply.github.comChris Boesch <48591413+chrboesch@users.noreply.github.com> 2024-11-26 14:03:48+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-11-26 13:03:48+00:00
log87863a834b074311d70ec4dd3b10c74705fff54d
tree850e2a8d256ef3dedea40ae3847870ba77477fe8
parentb0dcce93f7fca9ee4f8e4f2c0a34523b91b50c46
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.math.complex: Add squared magnitude function (#21998)


1 files changed, 11 insertions(+), 0 deletions(-)

lib/std/math/complex.zig+11
......@@ -115,6 +115,10 @@ pub fn Complex(comptime T: type) type {
115115 pub fn magnitude(self: Self) T {
116116 return @sqrt(self.re * self.re + self.im * self.im);
117117 }
118
119 pub fn squaredMagnitude(self: Self) T {
120 return self.re * self.re + self.im * self.im;
121 }
118122 };
119123}
120124
......@@ -189,6 +193,13 @@ test "magnitude" {
189193 try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));
190194}
191195
196test "squaredMagnitude" {
197 const a = Complex(f32).init(5, 3);
198 const c = a.squaredMagnitude();
199
200 try testing.expect(math.approxEqAbs(f32, c, math.pow(f32, a.magnitude(), 2), epsilon));
201}
202
192203test {
193204 _ = @import("complex/abs.zig");
194205 _ = @import("complex/acosh.zig");