Commit e72049bc61

Linus Groh <mail@linusgroh.de>
2023-10-27 15:50:39
std.math: Add isPositiveZero() and isNegativeZero()
1 parent b2ed2c4
lib/std/json/dynamic_test.zig
@@ -347,7 +347,7 @@ test "negative zero" {
     var parsed = try parseFromTokenSource(Value, testing.allocator, &reader, .{});
     defer parsed.deinit();
 
-    try testing.expect(parsed.value.float == 0 and std.math.signbit(parsed.value.float));
+    try testing.expect(std.math.isNegativeZero(parsed.value.float));
 }
 
 fn smallBufferJsonReader(allocator: Allocator, io_reader: anytype) JsonReader(16, @TypeOf(io_reader)) {
lib/std/math/asin.zig
@@ -169,15 +169,15 @@ test "math.asin64" {
 }
 
 test "math.asin32.special" {
-    try expect(asin32(0.0) == 0.0);
-    try expect(asin32(-0.0) == -0.0);
+    try expect(math.isPositiveZero(asin32(0.0)));
+    try expect(math.isNegativeZero(asin32(-0.0)));
     try expect(math.isNan(asin32(-2)));
     try expect(math.isNan(asin32(1.5)));
 }
 
 test "math.asin64.special" {
-    try expect(asin64(0.0) == 0.0);
-    try expect(asin64(-0.0) == -0.0);
+    try expect(math.isPositiveZero(asin64(0.0)));
+    try expect(math.isNegativeZero(asin64(-0.0)));
     try expect(math.isNan(asin64(-2)));
     try expect(math.isNan(asin64(1.5)));
 }
lib/std/math/asinh.zig
@@ -111,16 +111,16 @@ test "math.asinh64" {
 }
 
 test "math.asinh32.special" {
-    try expect(asinh32(0.0) == 0.0);
-    try expect(@as(u32, @bitCast(asinh32(-0.0))) == @as(u32, 0x80000000));
+    try expect(math.isPositiveZero(asinh32(0.0)));
+    try expect(math.isNegativeZero(asinh32(-0.0)));
     try expect(math.isPositiveInf(asinh32(math.inf(f32))));
     try expect(math.isNegativeInf(asinh32(-math.inf(f32))));
     try expect(math.isNan(asinh32(math.nan(f32))));
 }
 
 test "math.asinh64.special" {
-    try expect(asinh64(0.0) == 0.0);
-    try expect(@as(u64, @bitCast(asinh64(-0.0))) == @as(u64, 0x8000000000000000));
+    try expect(math.isPositiveZero(asinh64(0.0)));
+    try expect(math.isNegativeZero(asinh64(-0.0)));
     try expect(math.isPositiveInf(asinh64(math.inf(f64))));
     try expect(math.isNegativeInf(asinh64(-math.inf(f64))));
     try expect(math.isNan(asinh64(math.nan(f64))));
lib/std/math/atan.zig
@@ -239,8 +239,8 @@ test "math.atan64" {
 test "math.atan32.special" {
     const epsilon = 0.000001;
 
-    try expect(atan32(0.0) == 0.0);
-    try expect(atan32(-0.0) == -0.0);
+    try expect(math.isPositiveZero(atan32(0.0)));
+    try expect(math.isNegativeZero(atan32(-0.0)));
     try expect(math.approxEqAbs(f32, atan32(math.inf(f32)), math.pi / 2.0, epsilon));
     try expect(math.approxEqAbs(f32, atan32(-math.inf(f32)), -math.pi / 2.0, epsilon));
 }
@@ -248,8 +248,8 @@ test "math.atan32.special" {
 test "math.atan64.special" {
     const epsilon = 0.000001;
 
-    try expect(atan64(0.0) == 0.0);
-    try expect(atan64(-0.0) == -0.0);
+    try expect(math.isPositiveZero(atan64(0.0)));
+    try expect(math.isNegativeZero(atan64(-0.0)));
     try expect(math.approxEqAbs(f64, atan64(math.inf(f64)), math.pi / 2.0, epsilon));
     try expect(math.approxEqAbs(f64, atan64(-math.inf(f64)), -math.pi / 2.0, epsilon));
 }
lib/std/math/cbrt.zig
@@ -127,7 +127,7 @@ test "math.cbrt" {
 test "math.cbrt32" {
     const epsilon = 0.000001;
 
-    try expect(cbrt32(0.0) == 0.0);
+    try expect(math.isPositiveZero(cbrt32(0.0)));
     try expect(math.approxEqAbs(f32, cbrt32(0.2), 0.584804, epsilon));
     try expect(math.approxEqAbs(f32, cbrt32(0.8923), 0.962728, epsilon));
     try expect(math.approxEqAbs(f32, cbrt32(1.5), 1.144714, epsilon));
@@ -138,7 +138,7 @@ test "math.cbrt32" {
 test "math.cbrt64" {
     const epsilon = 0.000001;
 
-    try expect(cbrt64(0.0) == 0.0);
+    try expect(math.isPositiveZero(cbrt64(0.0)));
     try expect(math.approxEqAbs(f64, cbrt64(0.2), 0.584804, epsilon));
     try expect(math.approxEqAbs(f64, cbrt64(0.8923), 0.962728, epsilon));
     try expect(math.approxEqAbs(f64, cbrt64(1.5), 1.144714, epsilon));
@@ -147,7 +147,7 @@ test "math.cbrt64" {
 }
 
 test "math.cbrt.special" {
-    try expect(cbrt32(0.0) == 0.0);
+    try expect(math.isPositiveZero(cbrt32(0.0)));
     try expect(@as(u32, @bitCast(cbrt32(-0.0))) == @as(u32, 0x80000000));
     try expect(math.isPositiveInf(cbrt32(math.inf(f32))));
     try expect(math.isNegativeInf(cbrt32(-math.inf(f32))));
@@ -155,8 +155,8 @@ test "math.cbrt.special" {
 }
 
 test "math.cbrt64.special" {
-    try expect(cbrt64(0.0) == 0.0);
-    try expect(@as(u64, @bitCast(cbrt64(-0.0))) == @as(u64, 0x8000000000000000));
+    try expect(math.isPositiveZero(cbrt64(0.0)));
+    try expect(math.isNegativeZero(cbrt64(-0.0)));
     try expect(math.isPositiveInf(cbrt64(math.inf(f64))));
     try expect(math.isNegativeInf(cbrt64(-math.inf(f64))));
     try expect(math.isNan(cbrt64(math.nan(f64))));
lib/std/math/expm1.zig
@@ -293,7 +293,7 @@ test "math.exp1m" {
 test "math.expm1_32" {
     const epsilon = 0.000001;
 
-    try expect(expm1_32(0.0) == 0.0);
+    try expect(math.isPositiveZero(expm1_32(0.0)));
     try expect(math.approxEqAbs(f32, expm1_32(0.0), 0.0, epsilon));
     try expect(math.approxEqAbs(f32, expm1_32(0.2), 0.221403, epsilon));
     try expect(math.approxEqAbs(f32, expm1_32(0.8923), 1.440737, epsilon));
@@ -303,7 +303,7 @@ test "math.expm1_32" {
 test "math.expm1_64" {
     const epsilon = 0.000001;
 
-    try expect(expm1_64(0.0) == 0.0);
+    try expect(math.isPositiveZero(expm1_64(0.0)));
     try expect(math.approxEqAbs(f64, expm1_64(0.0), 0.0, epsilon));
     try expect(math.approxEqAbs(f64, expm1_64(0.2), 0.221403, epsilon));
     try expect(math.approxEqAbs(f64, expm1_64(0.8923), 1.440737, epsilon));
lib/std/math/iszero.zig
@@ -0,0 +1,41 @@
+const std = @import("../std.zig");
+const math = std.math;
+const expect = std.testing.expect;
+
+/// Returns whether x is positive zero.
+pub inline fn isPositiveZero(x: anytype) bool {
+    const T = @TypeOf(x);
+    const bit_count = @typeInfo(T).Float.bits;
+    const TBits = std.meta.Int(.unsigned, bit_count);
+    return @as(TBits, @bitCast(x)) == @as(TBits, 0);
+}
+
+/// Returns whether x is negative zero.
+pub inline fn isNegativeZero(x: anytype) bool {
+    const T = @TypeOf(x);
+    const bit_count = @typeInfo(T).Float.bits;
+    const TBits = std.meta.Int(.unsigned, bit_count);
+    return @as(TBits, @bitCast(x)) == @as(TBits, 1) << (bit_count - 1);
+}
+
+test isPositiveZero {
+    inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
+        try expect(isPositiveZero(@as(T, 0.0)));
+        try expect(!isPositiveZero(@as(T, -0.0)));
+        try expect(!isPositiveZero(math.floatMin(T)));
+        try expect(!isPositiveZero(math.floatMax(T)));
+        try expect(!isPositiveZero(math.inf(T)));
+        try expect(!isPositiveZero(-math.inf(T)));
+    }
+}
+
+test isNegativeZero {
+    inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
+        try expect(isNegativeZero(@as(T, -0.0)));
+        try expect(!isNegativeZero(@as(T, 0.0)));
+        try expect(!isNegativeZero(math.floatMin(T)));
+        try expect(!isNegativeZero(math.floatMax(T)));
+        try expect(!isNegativeZero(math.inf(T)));
+        try expect(!isNegativeZero(-math.inf(T)));
+    }
+}
lib/std/math/log1p.zig
@@ -212,8 +212,8 @@ test "math.log1p_64" {
 
 test "math.log1p_32.special" {
     try expect(math.isPositiveInf(log1p_32(math.inf(f32))));
-    try expect(log1p_32(0.0) == 0.0);
-    try expect(log1p_32(-0.0) == -0.0);
+    try expect(math.isPositiveZero(log1p_32(0.0)));
+    try expect(math.isNegativeZero(log1p_32(-0.0)));
     try expect(math.isNegativeInf(log1p_32(-1.0)));
     try expect(math.isNan(log1p_32(-2.0)));
     try expect(math.isNan(log1p_32(math.nan(f32))));
@@ -221,8 +221,8 @@ test "math.log1p_32.special" {
 
 test "math.log1p_64.special" {
     try expect(math.isPositiveInf(log1p_64(math.inf(f64))));
-    try expect(log1p_64(0.0) == 0.0);
-    try expect(log1p_64(-0.0) == -0.0);
+    try expect(math.isPositiveZero(log1p_64(0.0)));
+    try expect(math.isNegativeZero(log1p_64(-0.0)));
     try expect(math.isNegativeInf(log1p_64(-1.0)));
     try expect(math.isNan(log1p_64(-2.0)));
     try expect(math.isNan(log1p_64(math.nan(f64))));
lib/std/math/sinh.zig
@@ -123,16 +123,16 @@ test "math.sinh64" {
 }
 
 test "math.sinh32.special" {
-    try expect(sinh32(0.0) == 0.0);
-    try expect(sinh32(-0.0) == -0.0);
+    try expect(math.isPositiveZero(sinh32(0.0)));
+    try expect(math.isNegativeZero(sinh32(-0.0)));
     try expect(math.isPositiveInf(sinh32(math.inf(f32))));
     try expect(math.isNegativeInf(sinh32(-math.inf(f32))));
     try expect(math.isNan(sinh32(math.nan(f32))));
 }
 
 test "math.sinh64.special" {
-    try expect(sinh64(0.0) == 0.0);
-    try expect(sinh64(-0.0) == -0.0);
+    try expect(math.isPositiveZero(sinh64(0.0)));
+    try expect(math.isNegativeZero(sinh64(-0.0)));
     try expect(math.isPositiveInf(sinh64(math.inf(f64))));
     try expect(math.isNegativeInf(sinh64(-math.inf(f64))));
     try expect(math.isNan(sinh64(math.nan(f64))));
lib/std/math/tanh.zig
@@ -135,16 +135,16 @@ test "math.tanh64" {
 }
 
 test "math.tanh32.special" {
-    try expect(tanh32(0.0) == 0.0);
-    try expect(tanh32(-0.0) == -0.0);
+    try expect(math.isPositiveZero(tanh32(0.0)));
+    try expect(math.isNegativeZero(tanh32(-0.0)));
     try expect(tanh32(math.inf(f32)) == 1.0);
     try expect(tanh32(-math.inf(f32)) == -1.0);
     try expect(math.isNan(tanh32(math.nan(f32))));
 }
 
 test "math.tanh64.special" {
-    try expect(tanh64(0.0) == 0.0);
-    try expect(tanh64(-0.0) == -0.0);
+    try expect(math.isPositiveZero(tanh64(0.0)));
+    try expect(math.isNegativeZero(tanh64(-0.0)));
     try expect(tanh64(math.inf(f64)) == 1.0);
     try expect(tanh64(-math.inf(f64)) == -1.0);
     try expect(math.isNan(tanh64(math.nan(f64))));
lib/std/math.zig
@@ -222,6 +222,8 @@ pub const isFinite = @import("math/isfinite.zig").isFinite;
 pub const isInf = @import("math/isinf.zig").isInf;
 pub const isPositiveInf = @import("math/isinf.zig").isPositiveInf;
 pub const isNegativeInf = @import("math/isinf.zig").isNegativeInf;
+pub const isPositiveZero = @import("math/iszero.zig").isPositiveZero;
+pub const isNegativeZero = @import("math/iszero.zig").isNegativeZero;
 pub const isNormal = @import("math/isnormal.zig").isNormal;
 pub const nextAfter = @import("math/nextafter.zig").nextAfter;
 pub const signbit = @import("math/signbit.zig").signbit;