diff options
| author | Marc Tiehuis <marctiehuis@gmail.com> | 2017-06-16 20:26:10 +1200 |
|---|---|---|
| committer | Marc Tiehuis <marctiehuis@gmail.com> | 2017-06-16 20:32:31 +1200 |
| commit | 4c16f9a3c35b23b9917f2a27b91ba8cd20e6fd82 (patch) | |
| tree | 778f0f06734f7dc17e9269ee1cf5b513f7b504c0 /std/math/isfinite.zig | |
| parent | 865b53f2860405a718262abf9a794d2bf9529dbc (diff) | |
| download | zig-4c16f9a3c35b23b9917f2a27b91ba8cd20e6fd82.tar.gz zig-4c16f9a3c35b23b9917f2a27b91ba8cd20e6fd82.zip | |
Add math library
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.
Diffstat (limited to 'std/math/isfinite.zig')
| -rw-r--r-- | std/math/isfinite.zig | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/std/math/isfinite.zig b/std/math/isfinite.zig new file mode 100644 index 0000000000..a820cb2f52 --- /dev/null +++ b/std/math/isfinite.zig @@ -0,0 +1,30 @@ +const math = @import("index.zig"); +const assert = @import("../debug.zig").assert; + +pub fn isFinite(x: var) -> bool { + const T = @typeOf(x); + switch (T) { + f32 => { + const bits = @bitCast(u32, x); + bits & 0x7FFFFFFF < 0x7F800000 + }, + f64 => { + const bits = @bitCast(u64, x); + bits & (@maxValue(u64) >> 1) < (0x7FF << 52) + }, + else => { + @compileError("isFinite not implemented for " ++ @typeName(T)); + }, + } +} + +test "isFinite" { + assert(isFinite(f32(0.0))); + assert(isFinite(f32(-0.0))); + assert(isFinite(f64(0.0))); + assert(isFinite(f64(-0.0))); + assert(!isFinite(math.inf(f32))); + assert(!isFinite(-math.inf(f32))); + assert(!isFinite(math.inf(f64))); + assert(!isFinite(-math.inf(f64))); +} |
