mirror of
https://github.com/ziglang/zig.git
synced 2024-11-29 16:42:31 +00:00
4c16f9a3c3
This covers the majority of the functions as covered by the C99 specification for a math library. Code is adapted primarily from musl libc, with the pow and standard trigonometric functions adapted from the Go stdlib. Changes: - Remove assert expose in index and import as needed. - Add float log function and merge with existing base 2 integer implementation. See https://github.com/tiehuis/zig-fmath. See #374.
37 lines
786 B
Zig
37 lines
786 B
Zig
const math = @import("index.zig");
|
|
const assert = @import("../debug.zig").assert;
|
|
|
|
pub fn signbit(x: var) -> bool {
|
|
const T = @typeOf(x);
|
|
switch (T) {
|
|
f32 => @inlineCall(signbit32, x),
|
|
f64 => @inlineCall(signbit64, x),
|
|
else => @compileError("signbit not implemented for " ++ @typeName(T)),
|
|
}
|
|
}
|
|
|
|
fn signbit32(x: f32) -> bool {
|
|
const bits = @bitCast(u32, x);
|
|
bits >> 31 != 0
|
|
}
|
|
|
|
fn signbit64(x: f64) -> bool {
|
|
const bits = @bitCast(u64, x);
|
|
bits >> 63 != 0
|
|
}
|
|
|
|
test "signbit" {
|
|
assert(signbit(f32(4.0)) == signbit32(4.0));
|
|
assert(signbit(f64(4.0)) == signbit64(4.0));
|
|
}
|
|
|
|
test "signbit32" {
|
|
assert(!signbit32(4.0));
|
|
assert(signbit32(-3.0));
|
|
}
|
|
|
|
test "signbit64" {
|
|
assert(!signbit64(4.0));
|
|
assert(signbit64(-3.0));
|
|
}
|