Repository navigation
A number becomes text as JS writes it: the shortest round-trip digits (#780) - #783
Merged
Merged
Conversation
…#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
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>
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.
Fixes #780.
A
numberbecame text through C's%g, which keeps 6 significant digits (0.1 + 0.2printed0.3,2 ** 53 + 2printed9.0072e+015,1e-7printed1e-007). That path servesprint,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-Notherwise,NaN,Infinity,-Infinity, and"0"for-0.Changes
ConvertLogic.h:f64ToStringallocates 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_checkLNK2005 problem). It tries%.*ewith p = 0..16 decimals untilstrtodreads back the same double, then lays out the digits and exponent itself, so the CRT's exponent width does not matter. It uses onlysnprintf/sprintf_sandstrtod, which every target already links (ConvertF, parseFloat).jit.cpp: under the Windows JIT,sprintf_sandstrtodwere bound to a CRT DLL that is not tslang.exe's static UCRT. That CRT's%.*eis not correctly rounded (155280381172789.87instead of.88) and writes 3-digit exponents. Both are now bound to the static CRT, asputs/printf/memcpy_salready were.00number_to_string.ts(test-compile-andtest-jit-): the cases from the issue,""+x, a template,<string>x, literals andprint.String()comes from the default library and these tests build without it; DefaultLib'sNumber.toString(10)forwards to the same cast.Verification
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=llvmfor i686-pc-windows-msvc, x86_64-linux-gnu, aarch64-linux-android, wasm32 and wasm32-wasi: the helper isdefine internal, with the target's index width.--diJIT and--emit=obj --dialso build and run.ctest -C Release -j 16): 6689/6689 passed. No existing expected output changed.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
123456789012345680000evaluates to-5670419503621181000.jit.cppdescribes it with the host's%.17g, not the JS format.🤖 Generated with Claude Code