Repository navigation
fix(number): keep the sign of -0 and do not cap the exponent below the mantissa length - #248
Open
yuxi-liu-wired wants to merge 2 commits into
Open
yuxi-liu-wired wants to merge 2 commits into
yuxi-liu-wired wants to merge 2 commits into
Conversation
|
|
yuxi-liu-wired
force-pushed
the
pr/numbers
branch
from
October 1, 2026 20:23
ca72ff7 to
744f524
Compare
`-0.0`, `-0e5` and `-0` were parsed as positive zero: every zero
fast path in `parse_number` and `parse_number_unchecked` returned
`ParserNumber::Float(0.0)` without looking at `negative`, although the
literal is IEEE 754 negative zero (as `str::parse::<f64>` reads it, and
as `parse_float32` in the same crate already returns).
sonic_rs::from_str::<f64>("-0.0") 0.0 -> -0.0
sonic_rs::from_str::<Value>("-0") 0.0 -> -0.0
to_string(&from_str::<Value>("-0.0")) "0.0" -> "-0.0"
The sign survives `Value` round trips, so serializing a parsed `-0.0`
now gives back `-0.0`, as serde_json does.
Tests: `test_parse_number_negative_zero` (sonic-number, checked and
unchecked paths) and `test_negative_zero_keeps_sign` (public API: f64,
Value, round trip, inside an array). Both fail without the change.
Found by a three-way differential fuzzer (serde_json, simd-json,
sonic-rs).
Co-authored-by: TRAE CLI <[email protected]>
`parse_exponent` stops accumulating at 1000, so `e-30800000` and
`e-4294967296` both become `e-1000`. The exponent is then combined with
the number of digits of the mantissa, and for a mantissa longer than
about 1000 digits the capped value is no longer far enough out of
range:
"1{4000 ones}e-4294967296" 1.1111111111111111e-295 (is 0)
"1{4000 ones}e-30800000" error: not finite (is 0)
"0.{4000 zeros}1e4294967296" 1e293 (out of range)
"0.{4000 zeros}1e30800000" 0.0 (out of range)
Rust's `str::parse::<f64>` and serde_json give the right results. The
cap is raised to 0x10000, the same bound `core::num::dec2flt` uses: the
exponent still cannot overflow an i32, and no JSON text reaching the
parser has enough digits to bring it back into range.
Test: `test_parse_number_long_mantissa_huge_exponent` (fails before
this change). A sweep of 3840 combinations (mantissa length x exponent
x sign, f64/f32/Value) now matches `str::parse` exactly; 8 differed
before.
Found by a three-way differential fuzzer (serde_json, simd-json,
sonic-rs, jiter).
Co-authored-by: TRAE CLI <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related fixes in one area, 2 commits (each with its own test).
fix: keep the sign of negative zero when parsing numbers
-0.0,-0e5and-0were parsed as positive zero: every zerofast path in
parse_numberandparse_number_uncheckedreturnedParserNumber::Float(0.0)without looking atnegative, although theliteral is IEEE 754 negative zero (as
str::parse::<f64>reads it, andas
parse_float32in the same crate already returns).The sign survives
Valueround trips, so serializing a parsed-0.0now gives back
-0.0, as serde_json does.Tests:
test_parse_number_negative_zero(sonic-number, checked andunchecked paths) and
test_negative_zero_keeps_sign(public API: f64,Value, round trip, inside an array). Both fail without the change.
fix: don't cap the exponent below the mantissa's digit count
parse_exponentstops accumulating at 1000, soe-30800000ande-4294967296both becomee-1000. The exponent is then combined withthe number of digits of the mantissa, and for a mantissa longer than
about 1000 digits the capped value is no longer far enough out of
range:
Rust's
str::parse::<f64>and serde_json give the right results. Thecap is raised to 0x10000, the same bound
core::num::dec2fltuses: theexponent still cannot overflow an i32, and no JSON text reaching the
parser has enough digits to bring it back into range.
Test:
test_parse_number_long_mantissa_huge_exponent(fails beforethis change). A sweep of 3840 combinations (mantissa length x exponent
x sign, f64/f32/Value) now matches
str::parseexactly; 8 differedbefore.
Testing
Each commit adds a regression test that fails before the change. The full test suite passes and
cargo fmt --checkis clean on the changed files.Found with a differential fuzzer comparing serde_json, simd-json, sonic-rs and jiter.