zig/std/math/signbit.zig
Andrew Kelley c2db077574
std.debug.assert: remove special case for test builds
Previously, std.debug.assert would `@panic` in test builds,
if the assertion failed. Now, it's always `unreachable`.

This makes release mode test builds more accurately test
the actual code that will be run.

However this requires tests to call `std.testing.expect`
rather than `std.debug.assert` to make sure output is correct.

Here is the explanation of when to use either one, copied from
the assert doc comments:

Inside a test block, it is best to use the `std.testing` module
rather than assert, because assert may not detect a test failure
in ReleaseFast and ReleaseSafe mode. Outside of a test block, assert
is the correct function to use.

closes #1304
2019-02-08 18:23:38 -05:00

50 lines
1.0 KiB
Zig

const std = @import("../index.zig");
const math = std.math;
const expect = std.testing.expect;
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;
}
fn signbit64(x: f64) bool {
const bits = @bitCast(u64, x);
return bits >> 63 != 0;
}
test "math.signbit" {
expect(signbit(f16(4.0)) == signbit16(4.0));
expect(signbit(f32(4.0)) == signbit32(4.0));
expect(signbit(f64(4.0)) == signbit64(4.0));
}
test "math.signbit16" {
expect(!signbit16(4.0));
expect(signbit16(-3.0));
}
test "math.signbit32" {
expect(!signbit32(4.0));
expect(signbit32(-3.0));
}
test "math.signbit64" {
expect(!signbit64(4.0));
expect(signbit64(-3.0));
}