diff --git a/CMakeLists.txt b/CMakeLists.txt index 39d12843be..22e17d84c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -444,9 +444,9 @@ set(ZIG_STAGE2_SOURCES "${CMAKE_SOURCE_DIR}/lib/std/math.zig" "${CMAKE_SOURCE_DIR}/lib/std/math/big.zig" "${CMAKE_SOURCE_DIR}/lib/std/math/big/int.zig" + "${CMAKE_SOURCE_DIR}/lib/std/math/float.zig" "${CMAKE_SOURCE_DIR}/lib/std/math/floor.zig" "${CMAKE_SOURCE_DIR}/lib/std/math/frexp.zig" - "${CMAKE_SOURCE_DIR}/lib/std/math/inf.zig" "${CMAKE_SOURCE_DIR}/lib/std/math/isinf.zig" "${CMAKE_SOURCE_DIR}/lib/std/math/isnan.zig" "${CMAKE_SOURCE_DIR}/lib/std/math/ln.zig" diff --git a/lib/std/math.zig b/lib/std/math.zig index 353b3539b9..1ef7ac9798 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -45,6 +45,7 @@ pub const floatTrueMin = @import("math/float.zig").floatTrueMin; pub const floatMin = @import("math/float.zig").floatMin; pub const floatMax = @import("math/float.zig").floatMax; pub const floatEps = @import("math/float.zig").floatEps; +pub const inf = @import("math/float.zig").inf; // TODO Replace with @compileError("deprecated for foobar") after 0.10.0 is released. pub 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 pub const f64_toint: comptime_float = 1.0 / f64_epsilon; // same as before pub const f80_toint = 1.0 / f80_epsilon; // same as before pub const f128_toint = 1.0 / f128_epsilon; // same as before +pub const inf_u16 = @bitCast(u16, inf_f16); // prev: @as(u16, 0x7C00) +pub const inf_f16 = inf(f16); // prev: @bitCast(f16, inf_u16) +pub const inf_u32 = @bitCast(u32, inf_f32); // prev: @as(u32, 0x7F800000) +pub const inf_f32 = inf(f32); // prev: @bitCast(f32, inf_u32) +pub const inf_u64 = @bitCast(u64, inf_f64); // prev: @as(u64, 0x7FF << 52) +pub const inf_f64 = inf(f64); // prev: @bitCast(f64, inf_u64) +pub const inf_f80 = inf(f80); // prev: make_f80(F80{ .fraction = 0x8000000000000000, .exp = 0x7fff }) +pub const inf_u128 = @bitCast(u128, inf_f128); // prev: @as(u128, 0x7fff0000000000000000000000000000) +pub const inf_f128 = inf(f128); // prev: @bitCast(f128, inf_u128) pub const epsilon = floatEps; // End of "soft deprecated" section @@ -81,28 +91,18 @@ pub const nan_f16 = @bitCast(f16, nan_u16); pub const qnan_u16 = @as(u16, 0x7E00); pub const qnan_f16 = @bitCast(f16, qnan_u16); -pub const inf_u16 = @as(u16, 0x7C00); -pub const inf_f16 = @bitCast(f16, inf_u16); - pub const nan_u32 = @as(u32, 0x7F800001); pub const nan_f32 = @bitCast(f32, nan_u32); pub const qnan_u32 = @as(u32, 0x7FC00000); pub const qnan_f32 = @bitCast(f32, qnan_u32); -pub const inf_u32 = @as(u32, 0x7F800000); -pub const inf_f32 = @bitCast(f32, inf_u32); - pub const nan_u64 = @as(u64, 0x7FF << 52) | 1; pub const nan_f64 = @bitCast(f64, nan_u64); pub const qnan_u64 = @as(u64, 0x7ff8000000000000); pub const qnan_f64 = @bitCast(f64, qnan_u64); -pub const inf_u64 = @as(u64, 0x7FF << 52); -pub const inf_f64 = @bitCast(f64, inf_u64); - -pub const inf_f80 = make_f80(F80{ .fraction = 0x8000000000000000, .exp = 0x7fff }); pub const nan_f80 = make_f80(F80{ .fraction = 0xA000000000000000, .exp = 0x7fff }); pub const qnan_f80 = make_f80(F80{ .fraction = 0xC000000000000000, .exp = 0x7fff }); @@ -112,12 +112,8 @@ pub const nan_f128 = @bitCast(f128, nan_u128); pub const qnan_u128 = @as(u128, 0x7fff8000000000000000000000000000); pub const qnan_f128 = @bitCast(f128, qnan_u128); -pub const inf_u128 = @as(u128, 0x7fff0000000000000000000000000000); -pub const inf_f128 = @bitCast(f128, inf_u128); - pub const nan = @import("math/nan.zig").nan; pub const snan = @import("math/nan.zig").snan; -pub const inf = @import("math/inf.zig").inf; /// Performs an approximate comparison of two floating point values `x` and `y`. /// Returns true if the absolute difference between them is less or equal than diff --git a/lib/std/math/float.zig b/lib/std/math/float.zig index 9858aeb161..6d9c17d2a2 100644 --- a/lib/std/math/float.zig +++ b/lib/std/math/float.zig @@ -92,6 +92,11 @@ pub fn floatEps(comptime T: type) T { return reconstructFloat(T, -(floatMantissaDigits(T) - 1), mantissaOne(T)); } +/// Returns the value inf for floating point type T. +pub fn inf(comptime T: type) T { + return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T)); +} + test "std.math.float" { inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| { // (1 +) for the sign bit, since it is separate from the other bits diff --git a/lib/std/math/inf.zig b/lib/std/math/inf.zig deleted file mode 100644 index fd7d7c4380..0000000000 --- a/lib/std/math/inf.zig +++ /dev/null @@ -1,14 +0,0 @@ -const std = @import("../std.zig"); -const math = std.math; - -/// Returns value inf for the type T. -pub fn inf(comptime T: type) T { - return switch (T) { - f16 => math.inf_f16, - f32 => math.inf_f32, - f64 => math.inf_f64, - f80 => math.inf_f80, - f128 => math.inf_f128, - else => @compileError("inf not implemented for " ++ @typeName(T)), - }; -} diff --git a/lib/std/math/isfinite.zig b/lib/std/math/isfinite.zig index 77aab8bf76..e9314213ce 100644 --- a/lib/std/math/isfinite.zig +++ b/lib/std/math/isfinite.zig @@ -9,11 +9,8 @@ pub fn isFinite(x: anytype) bool { if (@typeInfo(T) != .Float) { @compileError("isFinite not implemented for " ++ @typeName(T)); } - const exponent_bits = math.floatExponentBits(T); - const mantissa_bits = math.floatMantissaBits(T); - const all1s_exponent = ((1 << exponent_bits) - 1) << mantissa_bits; const remove_sign = ~@as(TBits, 0) >> 1; - return @bitCast(TBits, x) & remove_sign < all1s_exponent; + return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T)); } test "math.isFinite" {