Fix js_atod rounding when input has more than max_digits significant digits

This commit is contained in:
Jake Bailey 2026-04-21 17:38:11 -07:00
parent d7ae12ae71
commit 68bd122e5d
2 changed files with 10 additions and 2 deletions

6
dtoa.c

@ -1505,8 +1505,10 @@ double js_atod(const char *str, const char **pnext, int radix, int flags,
}
/* Use the extra digits for rounding if the base is a power of
two. Otherwise they are just truncated. */
if (radix_bits != 0 && extra_digits != 0) {
two. Otherwise use them as a sticky bit so that round-half-to-even
rounds correctly when there are non-zero digits beyond the
precision we kept. */
if (extra_digits != 0) {
tmp0->tab[0] |= 1;
}

@ -398,6 +398,12 @@ function test_number()
assert((1.3).toString(7), "1.2046204620462046205");
assert((1.3).toString(35), "1.ahhhhhhhhhm");
/* parsing must use digits beyond max_digits as a sticky bit so
that round-half-to-even rounds in the correct direction.
https://github.com/bellard/quickjs/issues/452 */
assert(+"100000000000000000000000.000000000000001",
1.0000000000000001e+23);
}
function test_eval2()