zig/lib/std/math/iszero.zig
mlugg 0fe3fd01dd
std: update std.builtin.Type fields to follow naming conventions
The compiler actually doesn't need any functional changes for this: Sema
does reification based on the tag indices of `std.builtin.Type` already!
So, no zig1.wasm update is necessary.

This change is necessary to disallow name clashes between fields and
decls on a type, which is a prerequisite of #9938.
2024-08-28 08:39:59 +01:00

42 lines
1.4 KiB
Zig

const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;
/// Returns whether x is positive zero.
pub inline fn isPositiveZero(x: anytype) bool {
const T = @TypeOf(x);
const bit_count = @typeInfo(T).float.bits;
const TBits = std.meta.Int(.unsigned, bit_count);
return @as(TBits, @bitCast(x)) == @as(TBits, 0);
}
/// Returns whether x is negative zero.
pub inline fn isNegativeZero(x: anytype) bool {
const T = @TypeOf(x);
const bit_count = @typeInfo(T).float.bits;
const TBits = std.meta.Int(.unsigned, bit_count);
return @as(TBits, @bitCast(x)) == @as(TBits, 1) << (bit_count - 1);
}
test isPositiveZero {
inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
try expect(isPositiveZero(@as(T, 0.0)));
try expect(!isPositiveZero(@as(T, -0.0)));
try expect(!isPositiveZero(math.floatMin(T)));
try expect(!isPositiveZero(math.floatMax(T)));
try expect(!isPositiveZero(math.inf(T)));
try expect(!isPositiveZero(-math.inf(T)));
}
}
test isNegativeZero {
inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
try expect(isNegativeZero(@as(T, -0.0)));
try expect(!isNegativeZero(@as(T, 0.0)));
try expect(!isNegativeZero(math.floatMin(T)));
try expect(!isNegativeZero(math.floatMax(T)));
try expect(!isNegativeZero(math.inf(T)));
try expect(!isNegativeZero(-math.inf(T)));
}
}