diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-09-26 01:54:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-26 01:54:45 -0400 |
| commit | 68bb3945708c43109c48bda3664176307d45b62c (patch) | |
| tree | afb9731e10cef9d192560b52cd9ae2cf179775c4 /lib/std/math/isinf.zig | |
| parent | 6128bc728d1e1024a178c16c2149f5b1a167a013 (diff) | |
| parent | 4637e8f9699af9c3c6cf4df50ef5bb67c7a318a4 (diff) | |
| download | zig-68bb3945708c43109c48bda3664176307d45b62c.tar.gz zig-68bb3945708c43109c48bda3664176307d45b62c.zip | |
Merge pull request #3315 from ziglang/mv-std-lib
Move std/ to lib/std/
Diffstat (limited to 'lib/std/math/isinf.zig')
| -rw-r--r-- | lib/std/math/isinf.zig | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/lib/std/math/isinf.zig b/lib/std/math/isinf.zig new file mode 100644 index 0000000000..37934f4cf4 --- /dev/null +++ b/lib/std/math/isinf.zig @@ -0,0 +1,131 @@ +const std = @import("../std.zig"); +const math = std.math; +const expect = std.testing.expect; +const maxInt = std.math.maxInt; + +/// Returns whether x is an infinity, ignoring sign. +pub fn isInf(x: var) bool { + const T = @typeOf(x); + switch (T) { + f16 => { + const bits = @bitCast(u16, x); + return bits & 0x7FFF == 0x7C00; + }, + f32 => { + const bits = @bitCast(u32, x); + return bits & 0x7FFFFFFF == 0x7F800000; + }, + f64 => { + const bits = @bitCast(u64, x); + return bits & (maxInt(u64) >> 1) == (0x7FF << 52); + }, + f128 => { + const bits = @bitCast(u128, x); + return bits & (maxInt(u128) >> 1) == (0x7FFF << 112); + }, + else => { + @compileError("isInf not implemented for " ++ @typeName(T)); + }, + } +} + +/// Returns whether x is an infinity with a positive sign. +pub fn isPositiveInf(x: var) bool { + const T = @typeOf(x); + switch (T) { + f16 => { + return @bitCast(u16, x) == 0x7C00; + }, + f32 => { + return @bitCast(u32, x) == 0x7F800000; + }, + f64 => { + return @bitCast(u64, x) == 0x7FF << 52; + }, + f128 => { + return @bitCast(u128, x) == 0x7FFF << 112; + }, + else => { + @compileError("isPositiveInf not implemented for " ++ @typeName(T)); + }, + } +} + +/// Returns whether x is an infinity with a negative sign. +pub fn isNegativeInf(x: var) bool { + const T = @typeOf(x); + switch (T) { + f16 => { + return @bitCast(u16, x) == 0xFC00; + }, + f32 => { + return @bitCast(u32, x) == 0xFF800000; + }, + f64 => { + return @bitCast(u64, x) == 0xFFF << 52; + }, + f128 => { + return @bitCast(u128, x) == 0xFFFF << 112; + }, + else => { + @compileError("isNegativeInf not implemented for " ++ @typeName(T)); + }, + } +} + +test "math.isInf" { + expect(!isInf(f16(0.0))); + expect(!isInf(f16(-0.0))); + expect(!isInf(f32(0.0))); + expect(!isInf(f32(-0.0))); + expect(!isInf(f64(0.0))); + expect(!isInf(f64(-0.0))); + expect(!isInf(f128(0.0))); + expect(!isInf(f128(-0.0))); + expect(isInf(math.inf(f16))); + expect(isInf(-math.inf(f16))); + expect(isInf(math.inf(f32))); + expect(isInf(-math.inf(f32))); + expect(isInf(math.inf(f64))); + expect(isInf(-math.inf(f64))); + expect(isInf(math.inf(f128))); + expect(isInf(-math.inf(f128))); +} + +test "math.isPositiveInf" { + expect(!isPositiveInf(f16(0.0))); + expect(!isPositiveInf(f16(-0.0))); + expect(!isPositiveInf(f32(0.0))); + expect(!isPositiveInf(f32(-0.0))); + expect(!isPositiveInf(f64(0.0))); + expect(!isPositiveInf(f64(-0.0))); + expect(!isPositiveInf(f128(0.0))); + expect(!isPositiveInf(f128(-0.0))); + expect(isPositiveInf(math.inf(f16))); + expect(!isPositiveInf(-math.inf(f16))); + expect(isPositiveInf(math.inf(f32))); + expect(!isPositiveInf(-math.inf(f32))); + expect(isPositiveInf(math.inf(f64))); + expect(!isPositiveInf(-math.inf(f64))); + expect(isPositiveInf(math.inf(f128))); + expect(!isPositiveInf(-math.inf(f128))); +} + +test "math.isNegativeInf" { + expect(!isNegativeInf(f16(0.0))); + expect(!isNegativeInf(f16(-0.0))); + expect(!isNegativeInf(f32(0.0))); + expect(!isNegativeInf(f32(-0.0))); + expect(!isNegativeInf(f64(0.0))); + expect(!isNegativeInf(f64(-0.0))); + expect(!isNegativeInf(f128(0.0))); + expect(!isNegativeInf(f128(-0.0))); + expect(!isNegativeInf(math.inf(f16))); + expect(isNegativeInf(-math.inf(f16))); + expect(!isNegativeInf(math.inf(f32))); + expect(isNegativeInf(-math.inf(f32))); + expect(!isNegativeInf(math.inf(f64))); + expect(isNegativeInf(-math.inf(f64))); + expect(!isNegativeInf(math.inf(f128))); + expect(isNegativeInf(-math.inf(f128))); +} |
