authorgravatar for hi@viri.moeviri <hi@viri.moe> 2022-04-07 02:34:10-06:00
committergravatar for hi@viri.moeviri <hi@viri.moe> 2022-04-07 02:38:31-06:00
logc5c62605341143b19accc00f28af1c46c0a416be
tree629209d9d13fcdfb0d2711deeb8f64c669987a59
parenta2f5f0da5c21dd89834ec2b284ef2e7d2f5851a2
signaturelock-open Commit is signed but in an unrecognized format.

std.math: generalise `inf`, even simpler `isFinite`


5 files changed, 17 insertions(+), 33 deletions(-)

CMakeLists.txt+1-1
...@@ -444,9 +444,9 @@ set(ZIG_STAGE2_SOURCES...@@ -444,9 +444,9 @@ set(ZIG_STAGE2_SOURCES
444 "${CMAKE_SOURCE_DIR}/lib/std/math.zig"444 "${CMAKE_SOURCE_DIR}/lib/std/math.zig"
445 "${CMAKE_SOURCE_DIR}/lib/std/math/big.zig"445 "${CMAKE_SOURCE_DIR}/lib/std/math/big.zig"
446 "${CMAKE_SOURCE_DIR}/lib/std/math/big/int.zig"446 "${CMAKE_SOURCE_DIR}/lib/std/math/big/int.zig"
447 "${CMAKE_SOURCE_DIR}/lib/std/math/float.zig"
447 "${CMAKE_SOURCE_DIR}/lib/std/math/floor.zig"448 "${CMAKE_SOURCE_DIR}/lib/std/math/floor.zig"
448 "${CMAKE_SOURCE_DIR}/lib/std/math/frexp.zig"449 "${CMAKE_SOURCE_DIR}/lib/std/math/frexp.zig"
449 "${CMAKE_SOURCE_DIR}/lib/std/math/inf.zig"
450 "${CMAKE_SOURCE_DIR}/lib/std/math/isinf.zig"450 "${CMAKE_SOURCE_DIR}/lib/std/math/isinf.zig"
451 "${CMAKE_SOURCE_DIR}/lib/std/math/isnan.zig"451 "${CMAKE_SOURCE_DIR}/lib/std/math/isnan.zig"
452 "${CMAKE_SOURCE_DIR}/lib/std/math/ln.zig"452 "${CMAKE_SOURCE_DIR}/lib/std/math/ln.zig"
lib/std/math.zig+10-14
...@@ -45,6 +45,7 @@ pub const floatTrueMin = @import("math/float.zig").floatTrueMin;...@@ -45,6 +45,7 @@ pub const floatTrueMin = @import("math/float.zig").floatTrueMin;
45pub const floatMin = @import("math/float.zig").floatMin;45pub const floatMin = @import("math/float.zig").floatMin;
46pub const floatMax = @import("math/float.zig").floatMax;46pub const floatMax = @import("math/float.zig").floatMax;
47pub const floatEps = @import("math/float.zig").floatEps;47pub const floatEps = @import("math/float.zig").floatEps;
48pub const inf = @import("math/float.zig").inf;
4849
49// TODO Replace with @compileError("deprecated for foobar") after 0.10.0 is released.50// TODO Replace with @compileError("deprecated for foobar") after 0.10.0 is released.
50pub const f16_true_min: comptime_float = floatTrueMin(f16); // prev: 0.00000005960464477539062551pub const f16_true_min: comptime_float = floatTrueMin(f16); // prev: 0.000000059604644775390625
...@@ -72,6 +73,15 @@ pub const f32_toint: comptime_float = 1.0 / f32_epsilon; // same as before...@@ -72,6 +73,15 @@ pub const f32_toint: comptime_float = 1.0 / f32_epsilon; // same as before
72pub const f64_toint: comptime_float = 1.0 / f64_epsilon; // same as before73pub const f64_toint: comptime_float = 1.0 / f64_epsilon; // same as before
73pub const f80_toint = 1.0 / f80_epsilon; // same as before74pub const f80_toint = 1.0 / f80_epsilon; // same as before
74pub const f128_toint = 1.0 / f128_epsilon; // same as before75pub const f128_toint = 1.0 / f128_epsilon; // same as before
76pub const inf_u16 = @bitCast(u16, inf_f16); // prev: @as(u16, 0x7C00)
77pub const inf_f16 = inf(f16); // prev: @bitCast(f16, inf_u16)
78pub const inf_u32 = @bitCast(u32, inf_f32); // prev: @as(u32, 0x7F800000)
79pub const inf_f32 = inf(f32); // prev: @bitCast(f32, inf_u32)
80pub const inf_u64 = @bitCast(u64, inf_f64); // prev: @as(u64, 0x7FF << 52)
81pub const inf_f64 = inf(f64); // prev: @bitCast(f64, inf_u64)
82pub const inf_f80 = inf(f80); // prev: make_f80(F80{ .fraction = 0x8000000000000000, .exp = 0x7fff })
83pub const inf_u128 = @bitCast(u128, inf_f128); // prev: @as(u128, 0x7fff0000000000000000000000000000)
84pub const inf_f128 = inf(f128); // prev: @bitCast(f128, inf_u128)
75pub const epsilon = floatEps;85pub const epsilon = floatEps;
76// End of "soft deprecated" section86// End of "soft deprecated" section
7787
...@@ -81,28 +91,18 @@ pub const nan_f16 = @bitCast(f16, nan_u16);...@@ -81,28 +91,18 @@ pub const nan_f16 = @bitCast(f16, nan_u16);
81pub const qnan_u16 = @as(u16, 0x7E00);91pub const qnan_u16 = @as(u16, 0x7E00);
82pub const qnan_f16 = @bitCast(f16, qnan_u16);92pub const qnan_f16 = @bitCast(f16, qnan_u16);
8393
84pub const inf_u16 = @as(u16, 0x7C00);
85pub const inf_f16 = @bitCast(f16, inf_u16);
86
87pub const nan_u32 = @as(u32, 0x7F800001);94pub const nan_u32 = @as(u32, 0x7F800001);
88pub const nan_f32 = @bitCast(f32, nan_u32);95pub const nan_f32 = @bitCast(f32, nan_u32);
8996
90pub const qnan_u32 = @as(u32, 0x7FC00000);97pub const qnan_u32 = @as(u32, 0x7FC00000);
91pub const qnan_f32 = @bitCast(f32, qnan_u32);98pub const qnan_f32 = @bitCast(f32, qnan_u32);
9299
93pub const inf_u32 = @as(u32, 0x7F800000);
94pub const inf_f32 = @bitCast(f32, inf_u32);
95
96pub const nan_u64 = @as(u64, 0x7FF << 52) | 1;100pub const nan_u64 = @as(u64, 0x7FF << 52) | 1;
97pub const nan_f64 = @bitCast(f64, nan_u64);101pub const nan_f64 = @bitCast(f64, nan_u64);
98102
99pub const qnan_u64 = @as(u64, 0x7ff8000000000000);103pub const qnan_u64 = @as(u64, 0x7ff8000000000000);
100pub const qnan_f64 = @bitCast(f64, qnan_u64);104pub const qnan_f64 = @bitCast(f64, qnan_u64);
101105
102pub const inf_u64 = @as(u64, 0x7FF << 52);
103pub const inf_f64 = @bitCast(f64, inf_u64);
104
105pub const inf_f80 = make_f80(F80{ .fraction = 0x8000000000000000, .exp = 0x7fff });
106pub const nan_f80 = make_f80(F80{ .fraction = 0xA000000000000000, .exp = 0x7fff });106pub const nan_f80 = make_f80(F80{ .fraction = 0xA000000000000000, .exp = 0x7fff });
107pub const qnan_f80 = make_f80(F80{ .fraction = 0xC000000000000000, .exp = 0x7fff });107pub const qnan_f80 = make_f80(F80{ .fraction = 0xC000000000000000, .exp = 0x7fff });
108108
...@@ -112,12 +112,8 @@ pub const nan_f128 = @bitCast(f128, nan_u128);...@@ -112,12 +112,8 @@ pub const nan_f128 = @bitCast(f128, nan_u128);
112pub const qnan_u128 = @as(u128, 0x7fff8000000000000000000000000000);112pub const qnan_u128 = @as(u128, 0x7fff8000000000000000000000000000);
113pub const qnan_f128 = @bitCast(f128, qnan_u128);113pub const qnan_f128 = @bitCast(f128, qnan_u128);
114114
115pub const inf_u128 = @as(u128, 0x7fff0000000000000000000000000000);
116pub const inf_f128 = @bitCast(f128, inf_u128);
117
118pub const nan = @import("math/nan.zig").nan;115pub const nan = @import("math/nan.zig").nan;
119pub const snan = @import("math/nan.zig").snan;116pub const snan = @import("math/nan.zig").snan;
120pub const inf = @import("math/inf.zig").inf;
121117
122/// Performs an approximate comparison of two floating point values `x` and `y`.118/// Performs an approximate comparison of two floating point values `x` and `y`.
123/// Returns true if the absolute difference between them is less or equal than119/// Returns true if the absolute difference between them is less or equal than
lib/std/math/float.zig+5
...@@ -92,6 +92,11 @@ pub fn floatEps(comptime T: type) T {...@@ -92,6 +92,11 @@ pub fn floatEps(comptime T: type) T {
92 return reconstructFloat(T, -(floatMantissaDigits(T) - 1), mantissaOne(T));92 return reconstructFloat(T, -(floatMantissaDigits(T) - 1), mantissaOne(T));
93}93}
9494
95/// Returns the value inf for floating point type T.
96pub fn inf(comptime T: type) T {
97 return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T));
98}
99
95test "std.math.float" {100test "std.math.float" {
96 inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| {101 inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| {
97 // (1 +) for the sign bit, since it is separate from the other bits102 // (1 +) for the sign bit, since it is separate from the other bits
lib/std/math/inf.zig deleted-14
...@@ -1,14 +0,0 @@
1const std = @import("../std.zig");
2const math = std.math;
3
4/// Returns value inf for the type T.
5pub fn inf(comptime T: type) T {
6 return switch (T) {
7 f16 => math.inf_f16,
8 f32 => math.inf_f32,
9 f64 => math.inf_f64,
10 f80 => math.inf_f80,
11 f128 => math.inf_f128,
12 else => @compileError("inf not implemented for " ++ @typeName(T)),
13 };
14}
lib/std/math/isfinite.zig+1-4
...@@ -9,11 +9,8 @@ pub fn isFinite(x: anytype) bool {...@@ -9,11 +9,8 @@ pub fn isFinite(x: anytype) bool {
9 if (@typeInfo(T) != .Float) {9 if (@typeInfo(T) != .Float) {
10 @compileError("isFinite not implemented for " ++ @typeName(T));10 @compileError("isFinite not implemented for " ++ @typeName(T));
11 }11 }
12 const exponent_bits = math.floatExponentBits(T);
13 const mantissa_bits = math.floatMantissaBits(T);
14 const all1s_exponent = ((1 << exponent_bits) - 1) << mantissa_bits;
15 const remove_sign = ~@as(TBits, 0) >> 1;12 const remove_sign = ~@as(TBits, 0) >> 1;
16 return @bitCast(TBits, x) & remove_sign < all1s_exponent;13 return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T));
17}14}
1815
19test "math.isFinite" {16test "math.isFinite" {