Skip to content

A number becomes text as JS writes it: the shortest round-trip digits (#780) - #783

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-number-to-string-780
Nov 8, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-number-to-string-780

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fixes #780.

A number became text through C's %g, which keeps 6 significant digits (0.1 + 0.2 printed 0.3, 2 ** 53 + 2 printed 9.0072e+015, 1e-7 printed 1e-007). That path serves print, console.log, String(), "" + x, templates and <string>x. It now follows Number::toString (ECMA-262): the shortest digits that round-trip, plain decimal for -6 < n <= 21, d.ddde+N / e-N otherwise, NaN, Infinity, -Infinity, and "0" for -0.

Changes

  • ConvertLogic.h: f64ToString allocates a 32-byte buffer and calls __ts_number_to_string(ptr, f64). This routine is generated once per module with internal linkage, so modules linked together do not collide (the ___range_check LNK2005 problem). It tries %.*e with p = 0..16 decimals until strtod reads back the same double, then lays out the digits and exponent itself, so the CRT's exponent width does not matter. It uses only snprintf/sprintf_s and strtod, which every target already links (ConvertF, parseFloat).
  • jit.cpp: under the Windows JIT, sprintf_s and strtod were bound to a CRT DLL that is not tslang.exe's static UCRT. That CRT's %.*e is not correctly rounded (155280381172789.87 instead of .88) and writes 3-digit exponents. Both are now bound to the static CRT, as puts/printf/memcpy_s already were.
  • New test 00number_to_string.ts (test-compile- and test-jit-): the cases from the issue, ""+x, a template, <string>x, literals and print. String() comes from the default library and these tests build without it; DefaultLib's Number.toString(10) forwards to the same cast.

Verification

  • About 9,000 doubles (random bit patterns, value ranges, edge cases such as 5e-324, 2.2250738585072014e-308, 1.7976931348623157e+308, 1e21, 1e23) compared with node 22: 0 differences in --emit=jit (gc, rc and own) and in an exe. Before the jit.cpp change, the JIT had 26 differences in 3,031 values.
  • --emit=llvm for i686-pc-windows-msvc, x86_64-linux-gnu, aarch64-linux-android, wasm32 and wasm32-wasi: the helper is define internal, with the target's index width. --di JIT and --emit=obj --di also build and run.
  • Full Windows ctest (ctest -C Release -j 16): 6689/6689 passed. No existing expected output changed.
  • DefaultLib tests.ps1 -Model rc (release passes, clean clone of main f5d8edb): compile 188/189, jit 188/189 (1 gc-only skip).

Not in this PR

  • An integer literal that does not fit in 64 bits wraps silently: 123456789012345680000 evaluates to -5670419503621181000.
  • When an uncaught exception throws a number, jit.cpp describes it with the host's %.17g, not the JS format.

🤖 Generated with Claude Code

…#780)

`"" + x`, templates, `<string>x`, print() and String() went through C's `%g`,
which keeps 6 significant digits: `0.1 + 0.2` printed 0.3, `2 ** 53 + 2`
9.0072e+015, `1e-7` 1e-007. Number::toString (ECMA-262) wants the fewest
digits that read back as the same double, plain decimal for a decimal
exponent n in -6 < n <= 21 and `d.ddde+N` otherwise.

The conversion now calls `__ts_number_to_string`, a routine generated once
per module with internal linkage. It tries `%.*e` with 0..16 decimals until
strtod reads the same double back, then lays out the digits and exponent
itself, so the CRT's exponent width does not matter. It needs only
snprintf/sprintf_s and strtod, which every target already links.

Under the JIT on Windows, sprintf_s and strtod bound to a CRT DLL that is
not tslang.exe's static UCRT. That CRT's `%.*e` is not correctly rounded
(155280381172789.87 instead of .88) and writes 3-digit exponents. jit.cpp
now binds both to the static CRT, as it already does for puts/printf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 1b089a6 into main Nov 8, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-number-to-string-780 branch November 8, 2026 15:31
ASDAlexander77 added a commit that referenced this pull request Nov 8, 2026
… as JS writes it (#785)

An integer literal that no 64-bit integer holds was cut to its low 64 bits
(123456789012345680000 read -5670419503621181000), and one past 128 bits
was an error. It is now a `number`, the nearest double, as in JS:
123456789012345680000, 2^64 -> 18446744073709552000, 1e313's digits ->
Infinity. A literal in [2^63, 2^64) stays a u64, now exactly: the signed
string parse made it 65 bits wide, which took a 128-bit type and read back
only its low word.

Under the Windows JIT, an uncaught thrown number was described with `%.17g`
(`9.9999999999999995e-08`). It now uses the shortest round-trip digits in JS
layout, as `"" + x` does since #783 (`uncaught exception: 1e-7`).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

Numbers are converted to strings with only 6 significant digits

1 participant