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
444444 "${CMAKE_SOURCE_DIR}/lib/std/math.zig"
445445 "${CMAKE_SOURCE_DIR}/lib/std/math/big.zig"
446446 "${CMAKE_SOURCE_DIR}/lib/std/math/big/int.zig"
447 "${CMAKE_SOURCE_DIR}/lib/std/math/float.zig"
447448 "${CMAKE_SOURCE_DIR}/lib/std/math/floor.zig"
448449 "${CMAKE_SOURCE_DIR}/lib/std/math/frexp.zig"
449 "${CMAKE_SOURCE_DIR}/lib/std/math/inf.zig"
450450 "${CMAKE_SOURCE_DIR}/lib/std/math/isinf.zig"
451451 "${CMAKE_SOURCE_DIR}/lib/std/math/isnan.zig"
452452 "${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;
4545pub const floatMin = @import("math/float.zig").floatMin;
4646pub const floatMax = @import("math/float.zig").floatMax;
4747pub const floatEps = @import("math/float.zig").floatEps;
48pub const inf = @import("math/float.zig").inf;
4849
4950// TODO Replace with @compileError("deprecated for foobar") after 0.10.0 is released.
5051pub 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
7273pub const f64_toint: comptime_float = 1.0 / f64_epsilon; // same as before
7374pub const f80_toint = 1.0 / f80_epsilon; // same as before
7475pub 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)
7585pub const epsilon = floatEps;
7686// End of "soft deprecated" section
7787
......@@ -81,28 +91,18 @@ pub const nan_f16 = @bitCast(f16, nan_u16);
8191pub const qnan_u16 = @as(u16, 0x7E00);
8292pub const qnan_f16 = @bitCast(f16, qnan_u16);
8393
84pub const inf_u16 = @as(u16, 0x7C00);
85pub const inf_f16 = @bitCast(f16, inf_u16);
86
8794pub const nan_u32 = @as(u32, 0x7F800001);
8895pub const nan_f32 = @bitCast(f32, nan_u32);
8996
9097pub const qnan_u32 = @as(u32, 0x7FC00000);
9198pub const qnan_f32 = @bitCast(f32, qnan_u32);
9299
93pub const inf_u32 = @as(u32, 0x7F800000);
94pub const inf_f32 = @bitCast(f32, inf_u32);
95
96100pub const nan_u64 = @as(u64, 0x7FF << 52) | 1;
97101pub const nan_f64 = @bitCast(f64, nan_u64);
98102
99103pub const qnan_u64 = @as(u64, 0x7ff8000000000000);
100104pub 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 });
106106pub const nan_f80 = make_f80(F80{ .fraction = 0xA000000000000000, .exp = 0x7fff });
107107pub const qnan_f80 = make_f80(F80{ .fraction = 0xC000000000000000, .exp = 0x7fff });
108108
......@@ -112,12 +112,8 @@ pub const nan_f128 = @bitCast(f128, nan_u128);
112112pub const qnan_u128 = @as(u128, 0x7fff8000000000000000000000000000);
113113pub const qnan_f128 = @bitCast(f128, qnan_u128);
114114
115pub const inf_u128 = @as(u128, 0x7fff0000000000000000000000000000);
116pub const inf_f128 = @bitCast(f128, inf_u128);
117
118115pub const nan = @import("math/nan.zig").nan;
119116pub const snan = @import("math/nan.zig").snan;
120pub const inf = @import("math/inf.zig").inf;
121117
122118/// Performs an approximate comparison of two floating point values `x` and `y`.
123119/// 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 {
9292 return reconstructFloat(T, -(floatMantissaDigits(T) - 1), mantissaOne(T));
9393}
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
95100test "std.math.float" {
96101 inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| {
97102 // (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 {
99 if (@typeInfo(T) != .Float) {
1010 @compileError("isFinite not implemented for " ++ @typeName(T));
1111 }
12 const exponent_bits = math.floatExponentBits(T);
13 const mantissa_bits = math.floatMantissaBits(T);
14 const all1s_exponent = ((1 << exponent_bits) - 1) << mantissa_bits;
1512 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));
1714}
1815
1916test "math.isFinite" {