mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 23:52:31 +00:00
parent
ca444e6191
commit
be36179064
@ -5,12 +5,18 @@ const assert = std.debug.assert;
|
||||
pub fn signbit(x: var) bool {
|
||||
const T = @typeOf(x);
|
||||
return switch (T) {
|
||||
f16 => signbit16(x),
|
||||
f32 => signbit32(x),
|
||||
f64 => signbit64(x),
|
||||
else => @compileError("signbit not implemented for " ++ @typeName(T)),
|
||||
};
|
||||
}
|
||||
|
||||
fn signbit16(x: f16) bool {
|
||||
const bits = @bitCast(u16, x);
|
||||
return bits >> 15 != 0;
|
||||
}
|
||||
|
||||
fn signbit32(x: f32) bool {
|
||||
const bits = @bitCast(u32, x);
|
||||
return bits >> 31 != 0;
|
||||
@ -22,10 +28,16 @@ fn signbit64(x: f64) bool {
|
||||
}
|
||||
|
||||
test "math.signbit" {
|
||||
assert(signbit(f16(4.0)) == signbit16(4.0));
|
||||
assert(signbit(f32(4.0)) == signbit32(4.0));
|
||||
assert(signbit(f64(4.0)) == signbit64(4.0));
|
||||
}
|
||||
|
||||
test "math.signbit16" {
|
||||
assert(!signbit16(4.0));
|
||||
assert(signbit16(-3.0));
|
||||
}
|
||||
|
||||
test "math.signbit32" {
|
||||
assert(!signbit32(4.0));
|
||||
assert(signbit32(-3.0));
|
||||
|
Loading…
Reference in New Issue
Block a user