Skip to content

Commit e522b3d

Browse files
committed
Add comptime_float support to std.math.isFinite
1 parent 850f70a commit e522b3d

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/std/math/isfinite.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ const expect = std.testing.expect;
44

55
/// Returns whether x is a finite value.
66
pub fn isFinite(x: anytype) bool {
7-
const T = @TypeOf(x);
7+
const T = if (@TypeOf(x) == comptime_float) f128 else @TypeOf(x);
88
const TBits = std.meta.Int(.unsigned, @typeInfo(T).float.bits);
99
const remove_sign = ~@as(TBits, 0) >> 1;
10-
return @as(TBits, @bitCast(x)) & remove_sign < @as(TBits, @bitCast(math.inf(T)));
10+
return @as(TBits, @bitCast(@as(T, x))) & remove_sign < @as(TBits, @bitCast(math.inf(T)));
1111
}
1212

1313
test isFinite {
14-
inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
14+
inline for ([_]type{ f16, f32, f64, f80, f128, comptime_float }) |T| {
1515
// normals
1616
try expect(isFinite(@as(T, 1.0)));
1717
try expect(isFinite(-@as(T, 1.0)));
@@ -25,6 +25,8 @@ test isFinite {
2525
try expect(isFinite(math.floatMin(T)));
2626
try expect(isFinite(math.floatMax(T)));
2727

28+
if (T == comptime_float) return;
29+
2830
// inf & nan
2931
try expect(!isFinite(math.inf(T)));
3032
try expect(!isFinite(-math.inf(T)));

0 commit comments

Comments
 (0)