std.math: simpler error handling

This commit is contained in:
alice 2022-05-17 22:04:12 +01:00
parent 70b6b98e91
commit 951ab802a3
No known key found for this signature in database
GPG Key ID: 4E37AC776C82FD93
4 changed files with 4 additions and 16 deletions

View File

@ -5,10 +5,7 @@ const expect = std.testing.expect;
/// Returns whether x is a finite value.
pub fn isFinite(x: anytype) bool {
const T = @TypeOf(x);
const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
if (@typeInfo(T) != .Float) {
@compileError("isFinite not implemented for " ++ @typeName(T));
}
const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
const remove_sign = ~@as(TBits, 0) >> 1;
return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T));
}

View File

@ -5,10 +5,7 @@ const expect = std.testing.expect;
/// Returns whether x is an infinity, ignoring sign.
pub fn isInf(x: anytype) bool {
const T = @TypeOf(x);
const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
if (@typeInfo(T) != .Float) {
@compileError("isInf not implemented for " ++ @typeName(T));
}
const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
const remove_sign = ~@as(TBits, 0) >> 1;
return @bitCast(TBits, x) & remove_sign == @bitCast(TBits, math.inf(T));
}

View File

@ -5,10 +5,7 @@ const expect = std.testing.expect;
/// Returns whether x is neither zero, subnormal, infinity, or NaN.
pub fn isNormal(x: anytype) bool {
const T = @TypeOf(x);
const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
if (@typeInfo(T) != .Float) {
@compileError("isNormal not implemented for " ++ @typeName(T));
}
const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
const increment_exp = 1 << math.floatMantissaBits(T);
const remove_sign = ~@as(TBits, 0) >> 1;

View File

@ -15,10 +15,7 @@ pub fn ldexp(x: anytype, n: i32) @TypeOf(x) {
var shift = n;
const T = @TypeOf(base);
const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
if (@typeInfo(T) != .Float) {
@compileError("ldexp not implemented for " ++ @typeName(T));
}
const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
const mantissa_bits = math.floatMantissaBits(T);
const exponent_min = math.floatExponentMin(T);