math.hypot: fix incorrect over/underflow behavior (#19472)

This commit is contained in:
expikr
2024-05-30 03:58:05 -06:00
committed by Andrew Kelley
parent ce9d2eda73
commit 0aeeff0d94
3 changed files with 114 additions and 145 deletions

View File

@@ -94,6 +94,19 @@ pub inline fn floatEps(comptime T: type) T {
return reconstructFloat(T, -floatFractionalBits(T), mantissaOne(T));
}
/// Returns the local epsilon of floating point type T.
pub inline fn floatEpsAt(comptime T: type, x: T) T {
switch (@typeInfo(T)) {
.Float => |F| {
const U: type = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = F.bits } });
const u: U = @bitCast(x);
const y: T = @bitCast(u ^ 1);
return @abs(x - y);
},
else => @compileError("floatEpsAt only supports floats"),
}
}
/// Returns the value inf for floating point type T.
pub inline fn inf(comptime T: type) T {
return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T));