Skip to content

Read large decimal exponents in full - #442

Open
lemire wants to merge 2 commits into
mainfrom
decimal-exponent-cutoff
Open

lemire wants to merge 2 commits into
mainfrom
decimal-exponent-cutoff

Conversation

@lemire

@lemire lemire commented Nov 27, 2026

Copy link
Copy Markdown
Member

The decimal parser stopped accumulating the explicit exponent once it reached 0x10000000 (268435456), but kept consuming its digits. A long significand can then cancel the truncated exponent:

input before expected
0. + 268435455 zeros + 1e2684354560 1.0, success inf, result_out_of_range
1 + 268435456 zeros + e-2684354560 1.0, success 0, result_out_of_range

We now stop at 2^59. The significand shifts the exponent by one per digit, far less than 2^59 for any string that fits in memory, so a larger exponent is out of range whatever the significand, and 10 * 2^59 + 9 does not overflow an int64_t. The fix is a single constant.

The new double.long_exponent test builds 256 MB inputs (skipped on 32-bit builds; about 3 seconds). Without the fix, it fails on the two inputs above.

Benchmarks

Linux, Intel Xeon Gold 6548N, realbenchmark pinned to one core, interleaved, median of 5 runs. canada.txt and mesh.txt have no exponents: instruction counts are unchanged. On 200k random doubles printed with %.17g (all with exponents):

compiler type i/f before i/f after c/f before c/f after
GCC 16.2 double 263.3 261.8 74.8 74.2
GCC 16.2 float 219.9 219.6 69.8 68.1
clang 23.1.5 double 289.6 290.6 79.0 78.6
clang 23.1.5 float 253.0 253.9 74.6 75.5

The parser stopped accumulating the explicit exponent once it reached
0x10000000 but kept consuming its digits. A long significand could then
cancel the truncated exponent: 0.(268435455 zeros)1e2684354560 parsed as
1.0 instead of overflowing, and 1(268435456 zeros)e-2684354560 parsed as
1.0 instead of underflowing.

We now stop at 2^59. The significand shifts the exponent by one per digit,
far less than 2^59 for any string that fits in memory, so a larger
exponent is out of range whatever the significand, and 10 * 2^59 + 9 does
not overflow an int64_t.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant