Skip to content

Commit 869bda3

Browse files
committed
Add comptime_float support to std.math.is(Positive/Negative)Zero
1 parent 2098310 commit 869bda3

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

lib/std/math/iszero.zig

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,50 @@ const expect = std.testing.expect;
55
/// Returns whether x is positive zero.
66
pub inline fn isPositiveZero(x: anytype) bool {
77
const T = @TypeOf(x);
8-
const bit_count = @typeInfo(T).float.bits;
8+
const bit_count, const F = switch (@typeInfo(T)) {
9+
.float => |float| .{ float.bits, T },
10+
.comptime_float => .{ 128, f128 },
11+
else => @compileError("unknown floating point type " ++ @typeName(T)),
12+
};
913
const TBits = std.meta.Int(.unsigned, bit_count);
10-
return @as(TBits, @bitCast(x)) == @as(TBits, 0);
14+
return @as(TBits, @bitCast(@as(F, x))) == @as(TBits, 0);
1115
}
1216

1317
/// Returns whether x is negative zero.
1418
pub inline fn isNegativeZero(x: anytype) bool {
1519
const T = @TypeOf(x);
16-
const bit_count = @typeInfo(T).float.bits;
20+
const bit_count, const F = switch (@typeInfo(T)) {
21+
.float => |float| .{ float.bits, T },
22+
.comptime_float => .{ 128, f128 },
23+
else => @compileError("unknown floating point type " ++ @typeName(T)),
24+
};
1725
const TBits = std.meta.Int(.unsigned, bit_count);
18-
return @as(TBits, @bitCast(x)) == @as(TBits, 1) << (bit_count - 1);
26+
return @as(TBits, @bitCast(@as(F, x))) == @as(TBits, 1) << (bit_count - 1);
1927
}
2028

2129
test isPositiveZero {
22-
inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
30+
inline for ([_]type{ f16, f32, f64, f80, f128, comptime_float }) |T| {
2331
try expect(isPositiveZero(@as(T, 0.0)));
2432
try expect(!isPositiveZero(@as(T, -0.0)));
2533
try expect(!isPositiveZero(math.floatMin(T)));
2634
try expect(!isPositiveZero(math.floatMax(T)));
35+
36+
if (T == comptime_float) return;
37+
2738
try expect(!isPositiveZero(math.inf(T)));
2839
try expect(!isPositiveZero(-math.inf(T)));
2940
}
3041
}
3142

3243
test isNegativeZero {
33-
inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
44+
inline for ([_]type{ f16, f32, f64, f80, f128, comptime_float }) |T| {
3445
try expect(isNegativeZero(@as(T, -0.0)));
3546
try expect(!isNegativeZero(@as(T, 0.0)));
3647
try expect(!isNegativeZero(math.floatMin(T)));
3748
try expect(!isNegativeZero(math.floatMax(T)));
49+
50+
if (T == comptime_float) return;
51+
3852
try expect(!isNegativeZero(math.inf(T)));
3953
try expect(!isNegativeZero(-math.inf(T)));
4054
}

0 commit comments

Comments
 (0)