mirror of
https://github.com/ziglang/zig.git
synced 2026-02-15 05:48:31 +00:00
fix large f128 values being incorrectly parsed as inf
Found while fuzzing. Previously 1.1897314953572317650857593266280070162E4932 was parsed as +inf, which caused issues for round-trip serialization of floats. Only f128 had issues, but have added other tests for all floating point large normals. The max_exponent for f128 was wrong, it is subtly different in the decimal code-path as it is based on where the decimal digit should go. This needs to be 2 greater than the max exponent (e.g. 308 or 4932) to work correctly (greater by 1, then we use a >= comparision). In addition, I've removed the redundant `optimize` constant which was only use for testing the slow path locally.
This commit is contained in:
parent
08e886b8fe
commit
ff3bf98345
@ -78,6 +78,13 @@ test "fmt.parseFloat nan and inf" {
|
||||
}
|
||||
}
|
||||
|
||||
test "fmt.parseFloat largest normals" {
|
||||
try expectEqual(@as(u16, @bitCast(try parseFloat(f16, "65504"))), 0x7bff);
|
||||
try expectEqual(@as(u32, @bitCast(try parseFloat(f32, "3.4028234664E38"))), 0x7f7f_ffff);
|
||||
try expectEqual(@as(u64, @bitCast(try parseFloat(f64, "1.7976931348623157E308"))), 0x7fef_ffff_ffff_ffff);
|
||||
try expectEqual(@as(u128, @bitCast(try parseFloat(f128, "1.1897314953572317650857593266280070162E4932"))), 0x7ffe_ffff_ffff_ffff_ffff_ffff_ffff_ffff);
|
||||
}
|
||||
|
||||
test "fmt.parseFloat #11169" {
|
||||
try expectEqual(try parseFloat(f128, "9007199254740993.0"), 9007199254740993.0);
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ pub fn Decimal(comptime T: type) type {
|
||||
pub const max_digits_without_overflow = if (MantissaT == u64) 19 else 38;
|
||||
pub const decimal_point_range = if (MantissaT == u64) 2047 else 32767;
|
||||
pub const min_exponent = if (MantissaT == u64) -324 else -4966;
|
||||
pub const max_exponent = if (MantissaT == u64) 310 else 4933;
|
||||
pub const max_exponent = if (MantissaT == u64) 310 else 4934;
|
||||
pub const max_decimal_digits = if (MantissaT == u64) 18 else 37;
|
||||
|
||||
/// The number of significant digits in the decimal.
|
||||
|
||||
@ -5,8 +5,6 @@ const convertEiselLemire = @import("convert_eisel_lemire.zig").convertEiselLemir
|
||||
const convertSlow = @import("convert_slow.zig").convertSlow;
|
||||
const convertHex = @import("convert_hex.zig").convertHex;
|
||||
|
||||
const optimize = true;
|
||||
|
||||
pub const ParseFloatError = error{
|
||||
InvalidCharacter,
|
||||
};
|
||||
@ -41,25 +39,23 @@ pub fn parseFloat(comptime T: type, s: []const u8) ParseFloatError!T {
|
||||
return convertHex(T, n);
|
||||
}
|
||||
|
||||
if (optimize) {
|
||||
if (convertFast(T, n)) |f| {
|
||||
return f;
|
||||
}
|
||||
if (convertFast(T, n)) |f| {
|
||||
return f;
|
||||
}
|
||||
|
||||
if (T == f16 or T == f32 or T == f64) {
|
||||
// If significant digits were truncated, then we can have rounding error
|
||||
// only if `mantissa + 1` produces a different result. We also avoid
|
||||
// redundantly using the Eisel-Lemire algorithm if it was unable to
|
||||
// correctly round on the first pass.
|
||||
if (convertEiselLemire(T, n.exponent, n.mantissa)) |bf| {
|
||||
if (!n.many_digits) {
|
||||
if (T == f16 or T == f32 or T == f64) {
|
||||
// If significant digits were truncated, then we can have rounding error
|
||||
// only if `mantissa + 1` produces a different result. We also avoid
|
||||
// redundantly using the Eisel-Lemire algorithm if it was unable to
|
||||
// correctly round on the first pass.
|
||||
if (convertEiselLemire(T, n.exponent, n.mantissa)) |bf| {
|
||||
if (!n.many_digits) {
|
||||
return bf.toFloat(T, n.negative);
|
||||
}
|
||||
if (convertEiselLemire(T, n.exponent, n.mantissa + 1)) |bf2| {
|
||||
if (bf.eql(bf2)) {
|
||||
return bf.toFloat(T, n.negative);
|
||||
}
|
||||
if (convertEiselLemire(T, n.exponent, n.mantissa + 1)) |bf2| {
|
||||
if (bf.eql(bf2)) {
|
||||
return bf.toFloat(T, n.negative);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user