authorgravatar for 77922942+expikr@users.noreply.github.comexpikr <77922942+expikr@users.noreply.github.com> 2024-01-20 14:32:07+08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-01-20 01:32:07-05:00
logb729a3f008304d464b431f8ac34ad16cde08ba7b
tree00b0c9c4d1c9c5a8f75bab77ce4b39a7748b173a
parent1a98fcd00abb7266418532a103214467497d58c6
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.math: make hypot infer type from argument (#17910)

using peer type resolution

3 files changed, 21 insertions(+), 14 deletions(-)

lib/std/math/complex/abs.zig+2-3
......@@ -5,9 +5,8 @@ const cmath = math.complex;
55const Complex = cmath.Complex;
66
77/// Returns the absolute value (modulus) of z.
8pub fn abs(z: anytype) @TypeOf(z.re) {
9 const T = @TypeOf(z.re);
10 return math.hypot(T, z.re, z.im);
8pub fn abs(z: anytype) @TypeOf(z.re, z.im) {
9 return math.hypot(z.re, z.im);
1110}
1211
1312const epsilon = 0.0001;
lib/std/math/complex/sqrt.zig+4-4
......@@ -56,13 +56,13 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
5656 const dy = @as(f64, y);
5757
5858 if (dx >= 0) {
59 const t = @sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
59 const t = @sqrt((dx + math.hypot(dx, dy)) * 0.5);
6060 return Complex(f32).init(
6161 @as(f32, @floatCast(t)),
6262 @as(f32, @floatCast(dy / (2.0 * t))),
6363 );
6464 } else {
65 const t = @sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
65 const t = @sqrt((-dx + math.hypot(dx, dy)) * 0.5);
6666 return Complex(f32).init(
6767 @as(f32, @floatCast(@abs(y) / (2.0 * t))),
6868 @as(f32, @floatCast(math.copysign(t, y))),
......@@ -112,10 +112,10 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
112112
113113 var result: Complex(f64) = undefined;
114114 if (x >= 0) {
115 const t = @sqrt((x + math.hypot(f64, x, y)) * 0.5);
115 const t = @sqrt((x + math.hypot(x, y)) * 0.5);
116116 result = Complex(f64).init(t, y / (2.0 * t));
117117 } else {
118 const t = @sqrt((-x + math.hypot(f64, x, y)) * 0.5);
118 const t = @sqrt((-x + math.hypot(x, y)) * 0.5);
119119 result = Complex(f64).init(@abs(y) / (2.0 * t), math.copysign(t, y));
120120 }
121121
lib/std/math/hypot.zig+15-7
......@@ -12,11 +12,15 @@ const maxInt = std.math.maxInt;
1212/// Returns sqrt(x * x + y * y), avoiding unnecessary overflow and underflow.
1313///
1414/// Special Cases:
15/// - hypot(+-inf, y) = +inf
16/// - hypot(x, +-inf) = +inf
17/// - hypot(nan, y) = nan
18/// - hypot(x, nan) = nan
19pub fn hypot(comptime T: type, x: T, y: T) T {
15///
16/// | x | y | hypot |
17/// |-------|-------|-------|
18/// | +inf | num | +inf |
19/// | num | +-inf | +inf |
20/// | nan | any | nan |
21/// | any | nan | nan |
22pub fn hypot(x: anytype, y: anytype) @TypeOf(x, y) {
23 const T = @TypeOf(x, y);
2024 return switch (T) {
2125 f32 => hypot32(x, y),
2226 f64 => hypot64(x, y),
......@@ -121,8 +125,12 @@ fn hypot64(x: f64, y: f64) f64 {
121125}
122126
123127test "math.hypot" {
124 try expect(hypot(f32, 0.0, -1.2) == hypot32(0.0, -1.2));
125 try expect(hypot(f64, 0.0, -1.2) == hypot64(0.0, -1.2));
128 const x32: f32 = 0.0;
129 const y32: f32 = -1.2;
130 const x64: f64 = 0.0;
131 const y64: f64 = -1.2;
132 try expect(hypot(x32, y32) == hypot32(0.0, -1.2));
133 try expect(hypot(x64, y64) == hypot64(0.0, -1.2));
126134}
127135
128136test "math.hypot32" {