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

using peer type resolution
This commit is contained in:
expikr 2024-01-20 14:32:07 +08:00 committed by GitHub
parent 1a98fcd00a
commit b729a3f008
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 14 deletions

View File

@ -5,9 +5,8 @@ const cmath = math.complex;
const Complex = cmath.Complex;
/// Returns the absolute value (modulus) of z.
pub fn abs(z: anytype) @TypeOf(z.re) {
const T = @TypeOf(z.re);
return math.hypot(T, z.re, z.im);
pub fn abs(z: anytype) @TypeOf(z.re, z.im) {
return math.hypot(z.re, z.im);
}
const epsilon = 0.0001;

View File

@ -56,13 +56,13 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
const dy = @as(f64, y);
if (dx >= 0) {
const t = @sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
const t = @sqrt((dx + math.hypot(dx, dy)) * 0.5);
return Complex(f32).init(
@as(f32, @floatCast(t)),
@as(f32, @floatCast(dy / (2.0 * t))),
);
} else {
const t = @sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
const t = @sqrt((-dx + math.hypot(dx, dy)) * 0.5);
return Complex(f32).init(
@as(f32, @floatCast(@abs(y) / (2.0 * t))),
@as(f32, @floatCast(math.copysign(t, y))),
@ -112,10 +112,10 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
var result: Complex(f64) = undefined;
if (x >= 0) {
const t = @sqrt((x + math.hypot(f64, x, y)) * 0.5);
const t = @sqrt((x + math.hypot(x, y)) * 0.5);
result = Complex(f64).init(t, y / (2.0 * t));
} else {
const t = @sqrt((-x + math.hypot(f64, x, y)) * 0.5);
const t = @sqrt((-x + math.hypot(x, y)) * 0.5);
result = Complex(f64).init(@abs(y) / (2.0 * t), math.copysign(t, y));
}

View File

@ -12,11 +12,15 @@ const maxInt = std.math.maxInt;
/// Returns sqrt(x * x + y * y), avoiding unnecessary overflow and underflow.
///
/// Special Cases:
/// - hypot(+-inf, y) = +inf
/// - hypot(x, +-inf) = +inf
/// - hypot(nan, y) = nan
/// - hypot(x, nan) = nan
pub fn hypot(comptime T: type, x: T, y: T) T {
///
/// | x | y | hypot |
/// |-------|-------|-------|
/// | +inf | num | +inf |
/// | num | +-inf | +inf |
/// | nan | any | nan |
/// | any | nan | nan |
pub fn hypot(x: anytype, y: anytype) @TypeOf(x, y) {
const T = @TypeOf(x, y);
return switch (T) {
f32 => hypot32(x, y),
f64 => hypot64(x, y),
@ -121,8 +125,12 @@ fn hypot64(x: f64, y: f64) f64 {
}
test "math.hypot" {
try expect(hypot(f32, 0.0, -1.2) == hypot32(0.0, -1.2));
try expect(hypot(f64, 0.0, -1.2) == hypot64(0.0, -1.2));
const x32: f32 = 0.0;
const y32: f32 = -1.2;
const x64: f64 = 0.0;
const y64: f64 = -1.2;
try expect(hypot(x32, y32) == hypot32(0.0, -1.2));
try expect(hypot(x64, y64) == hypot64(0.0, -1.2));
}
test "math.hypot32" {