2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
|
|
|
const assert = std.debug.assert;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn signbit(x: var) bool {
|
2017-06-16 01:26:10 -07:00
|
|
|
const T = @typeOf(x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => signbit32(x),
|
|
|
|
f64 => signbit64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("signbit not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn signbit32(x: f32) bool {
|
2017-06-16 01:26:10 -07:00
|
|
|
const bits = @bitCast(u32, x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return bits >> 31 != 0;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn signbit64(x: f64) bool {
|
2017-06-16 01:26:10 -07:00
|
|
|
const bits = @bitCast(u64, x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return bits >> 63 != 0;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.signbit" {
|
2017-06-16 01:26:10 -07:00
|
|
|
assert(signbit(f32(4.0)) == signbit32(4.0));
|
|
|
|
assert(signbit(f64(4.0)) == signbit64(4.0));
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.signbit32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
assert(!signbit32(4.0));
|
|
|
|
assert(signbit32(-3.0));
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.signbit64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
assert(!signbit64(4.0));
|
|
|
|
assert(signbit64(-3.0));
|
|
|
|
}
|