Commit 951ab802a3

alice <hi@dnydxn.moe>
2022-05-17 23:04:12
std.math: simpler error handling
1 parent 70b6b98
Changed files (4)
lib/std/math/isfinite.zig
@@ -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));
 }
lib/std/math/isinf.zig
@@ -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));
 }
lib/std/math/isnormal.zig
@@ -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;
lib/std/math/ldexp.zig
@@ -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);