mirror of
https://github.com/ziglang/zig.git
synced 2026-02-15 05:48:31 +00:00
std.math: simpler error handling
This commit is contained in:
parent
70b6b98e91
commit
951ab802a3
@ -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));
|
||||
}
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user