Repository navigation
prelude: Float.toString via Ryu (round-trip-safe, shortest representation) - #1288
Merged
Merged
Conversation
mbouaziz
force-pushed
the
fix-float-precision
branch
from
July 7, 2026 10:34
43c2ae1 to
ed97067
Compare
mbouaziz
added a commit
that referenced
this pull request
Nov 13, 2026
…round-trip + TOML float parsing) (#1287) ## What Two pre-existing float-parsing bugs share one root cause — reconstructing a double from its decimal digits with repeated `* 0.1`, which is not correctly rounded: 1. **WASM `String.toFloat`** (`SKIP_String__toFloat_raw`'s in-tree `atof` on SKIP32): `"0.3".toFloat()` yielded `0.30000000000000004`, round-trip (`toString().toFloat() == value`) failed for ~85% of sampled doubles, and the leading `-` was ignored, so every negative number parsed to `0` (`"-1.5"` → `0`). 2. **TOML `parseFloat`**: `"3.14"` parsed to `3.1400000000000006` instead of the nearest double. Both were masked while `Float.toString` produced only 9 significant digits; they surface once toString becomes exact (#1288). ## How - Vendor the Ryu library (`ryu/d2s.c` double→string and `ryu/s2d.c` string→double) from ulfjack/ryu @ `4c0618b0`, with `#ifdef SKIP32` guards so it builds under `-nostdlibinc`, and `RYU_ONLY_64_BIT_OPS` on SKIP32 so it needs no 128-bit compiler-rt helpers (`__multi3`, `__lshrti3`). `ryu/README.md` records the origin, license and local changes. (`d2s` is used by #1288; `s2d` here.) - SKIP32 `String.toFloat` uses Ryu `s2d` (correctly rounded, ~20 ns) for the inputs it supports (≤17 significant digits). For longer inputs it calls a new host import, `SKIP_js_parse_float`, implemented in `sk_runtime.ts` with `Number()` (correctly rounded for any input). The in-tree `atof` is removed. SKIP64 keeps libc `atof`, already correct. - TOML `parseFloat` keeps its validation but delegates the value to `String.toFloat` instead of reconstructing it by hand. **Wasm import:** the module now imports `SKIP_js_parse_float`. Every host (skdb, `@skipruntime/wasm`) gets it from `sk_runtime.ts`, which their builds copy in next to the module, so they stay in sync. A host that loads a new module with an old `sk_runtime.ts` would fail to instantiate. ## Tests - `runtime/tests/string_to_float.{c,mjs}`: `s2d` built with the Wasm runtime's flags, checked bit for bit against `Number()` (edge cases, 100k random decimal strings, shortest representations of 100k random doubles). Links without `--allow-undefined`, so it fails if `__multi3` & co. come back. - `runtime/tests/to_float_raw.{c,mjs}`: the real `string.c` built for Wasm, with the host import as in `sk_runtime.ts`; checks the `s2d` path and the host path against `Number()`. Fails on the previous `atof` (`-123456789012345678` → `0`). - `toml` `float-bits`: compares decoded bits, which the JSON fixtures cannot (they compare 9-digit `toString` output). Fails on the previous `parseFloat`. All three run in CI: the first two in the compiler job's runtime regressions (<1s), the last in the `toml` package tests. ## Choice of Ryu, license - **Compatibility with JS:** Ryu `d2s` gives the same shortest digits as JS `Number.prototype.toString` (0 differences over 7M doubles, including subnormals and `DBL_MAX`). Differences in layout (`1.0` vs `1`, exponent thresholds) belong to the #1288 formatter. - **License:** Ryu is dual Apache 2.0 / Boost 1.0; we use it under Boost, which is MIT-compatible and only requires keeping the notices in source copies. ## Validation Rebased onto `main` (one conflict in `prelude/build.sk`: `magic.c` is now Wasm-only since the build-id change; kept that and added the Ryu sources and `.include("runtime")`). `main`'s incremental prelude build (#1637) tracks `runtime/*.c`/`*.h` only, so `runtime/ryu/` is added to `RUNTIME_SRCS` (checked: touching `ryu/s2d.c` rebuilds the archive). - Vendored files compared with upstream @ `4c0618b0`: `d2s` and licenses byte-identical; `s2d.c`/`ryu_parse.h` differ only by the clang-format pass. - `s2d` vs libc `strtod`: 0 mismatches over ~25M random inputs, with both the native 128-bit path and the `RYU_ONLY_64_BIT_OPS` path used on Wasm (UBSan trap mode). - skdb npm package built locally and run in Node (real `sk_runtime.ts`): long `FLOAT` literals (`-123456789012345678`, `3.14159265358979323846`, …) insert as the nearest double. `skdb` Wasm module: the only new import is `SKIP_js_parse_float`; no `__multi3`/`__lshrti3`. - `@skip-wasm/std` builds and lints. - `runtime/tests/run.sh`, `prelude` (131/131) and `toml` (332/332) tests pass. - Not run locally: `make STAGE=1`, the skdb Wasm Playwright suite (CI runs both with the candidate toolchain since the runtime changed). After merge, the CI images need a `docker-publish` run (runtime change; see HACKING.md). ## AI assistance The original commits were written with Claude Code (Claude Opus 4.6/4.8). The rebase, review and the commits on top (tests, `.gitignore`, the Wasm fallback fix, `ryu/README.md`) were done with Mistral Vibe (Claude Opus 5.5, thinking: high); see the `Co-Authored-By` trailers.
mbouaziz
force-pushed
the
fix-float-precision
branch
4 times, most recently
from
November 16, 2026 14:24
befe56f to
f6648d8
Compare
Let the runtime handle Float.toString() instead of folding at compile time, avoiding precision differences between host and target compilers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace the pure-Skip splitFloat/normalizeFloat implementation (which used 1e9 scaling and could only produce 9 significant digits) with a C helper using Ryu's d2s_buffered_n() for shortest-representation formatting. This guarantees IEEE 754 double round-trip: toString().toFloat() == original for all finite values, while producing clean output (e.g., "0.1" not "0.10000000000000001"). No libc dependency, works in both SKIP64 and SKIP32. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Update expected values from the old 9-significant-digit output to Ryu's shortest round-trip representation, e.g.: - 5e-324: "5.0e-324" instead of "4.940656458e-324" - DBL_MAX: "1.7976931348623157e+308" instead of "1.797693135e+308" - 0.1 + 0.2: "0.30000000000000004" instead of "0.3" - 2^53: "9.007199254740992e+15" instead of "9.007199255e+15" Values that did not round-trip before (5e-324, the smallest normal, DBL_MAX, 2^53 and 2^53 - 1) are added to the round-trip test, and the TOML golden for long pi now has all its digits. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The name came from an earlier fixed-17-digit version; the function now returns the shortest round-trip representation (up to 17 digits). Also include ryu/ryu.h at the top of string.c with the other includes, drop the duplicate d2s_buffered_n prototype it already declares, note the buffer bounds, and give the real reason Float.toString is no longer constant-folded: folding would use the compiler's own Float.toString, i.e. that of the prelude it was built with, not the runtime's. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
Build SKIP_Float_toStringShortest for Wasm and compare it with the digits of JS Number.prototype.toString laid out in Skip's conventions (fixed notation for 1e-4 <= value < 1e15, ".0" for integers, e+N / e-0N / e-NN exponents), for edge cases and 200k random values; each result must also round-trip through Number(). Changing the fixed-notation bound to 1e16 makes it fail. Unlike the prelude's Float tests, it does not go through skc, so it is not affected by Float.toString constant folding in an older compiler. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
The toString tests called toString() on Float literals, which skc constant-folds with its own Float.toString: that of the prelude the compiler was built with, not the runtime under test. The candidate toolchain CI installs is built from the bootstrap IR, whose Float.toString is the old 9-digit one, so testToStringEdgeCases failed there (5e-324 -> "4.940656458e-324"). Parse each input with String.toFloat, which is never folded, so the tests exercise the runtime formatter with any compiler. With the previous Float.toString they now fail as expected. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
The test compares the text of readAlpha before and after an edit to check that its vtable slot is stable. Debug metadata (!dbg !N) is numbered per LLVM module, so when the edited createBeta ends up in the same module as readAlpha (as with this branch's smaller prelude), the numbers shift while the dispatch code is unchanged, and the test failed. Strip !dbg attachments with llvm_artifact_diff.without_dbg_attachments before comparing. The GEP offsets, i.e. the slot, are still compared: changing one still fails the test. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
Float.toString now prints the shortest round-trip representation instead of 9 significant digits, a visible change in output. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
Float.toString() is no longer constant-folded. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
Fixed notation had three hand-written cases (digits split by the point, an integer with ".0", an integer padded with zeros) plus a fourth for values below 1. A single loop over digit weights, from the highest (at least 10^0) to the lowest (at least 10^-1), covers all of them; the scientific exponent is written in one place. 109 -> 47 lines. Output is unchanged: byte-identical to the previous version on 18.6M values (random doubles, decimals and integers, and m * 10^e for every exponent), with UBSan. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
Negative values recurse on their magnitude, so -inf and -0.0 go through the same checks as inf and 0.0 instead of a separate -inf case. Also replace the Float.toString doc comment, which still described the old "%.17g" format. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
peepholeFloatToString returned its argument unchanged. List FloatToString with the other instructions the peephole leaves as is, and keep the comment explaining why it is not folded there. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
mbouaziz
force-pushed
the
fix-float-precision
branch
from
November 16, 2026 15:05
f6648d8 to
1d2c696
Compare
mbouaziz
added a commit
that referenced
this pull request
Nov 17, 2026
Rebuilt from ab4bad9 (main + the skargo --skcopt dedup fix) with bin/bump_bootstrap.sh: clean self-hosting rebuild through stage3 with an identical fixpoint, and the full compiler test suite (1766/1766) against the new stage0. The promoted IR carries the Ryu-based Float.toString from #1288, so the bootstrap compiler now formats floats exactly like the current runtime. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
mbouaziz
added a commit
that referenced
this pull request
Nov 17, 2026
Fold FloatToString(ConstantFloat) to a constant string again. This was safe once the bootstrap carries the same Float.toString as the target runtime: the bootstrap is only published after a compiler rebuilt from it passes the full test suite (bin/bump_bootstrap.sh), and the bump in this PR promotes an IR built from the Ryu-based prelude (#1288). Adds a Runtime test pinning the folded output of constant Float.toString (including -0.0, nan and inf) to the runtime's shortest round-trip form. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
mbouaziz
added a commit
that referenced
this pull request
Nov 17, 2026
…#1688) ## What 1. **Bump the bootstrap compiler** to `SkipLabs/skiplang-bootstrap@2ef40fd`, promoted from `ab4bad9c` (= current `main` + the skargo fix below) with `bin/bump_bootstrap.sh`. The promoted IR carries the Ryu-based `Float.toString` from #1288, so the bootstrap compiler now formats floats exactly like the current runtime. 2. **Restore the `FloatToString` peephole constant fold** (`FloatToString(ConstantFloat)` -> constant string), removed in #1288 because the old bootstrap folded with the old formatter while the runtime printed the new one. With the bumped bootstrap, compiler and target share the same `Float.toString`, so folding is consistent again. A new `Runtime/FloatToString` test pins the folded output of constant `Float.toString` (`0.1`, `-0.0`, `1.0e+100`, `nan`, `-inf`, ...) to the runtime's shortest round-trip form. 3. **Fix a latent skargo bug** that blocked the bump: the release profile started passing `--split-support-module` to skc itself (c5ff01a), while the compiler Makefile still forwarded it via `--skcopt` (needed only by the older bootstrap skargo). skc rejects a duplicated option, so every stage2+ build failed — including the stage3 verification in `bump_bootstrap.sh`. `Skc.process` now drops from `--skcopt` whatever the profile already sets, with regression tests. ## Validation - `bin/bump_bootstrap.sh` on `ab4bad9c`: clean rebuild from the promoted IR through **stage3** with an **identical self-hosting fixpoint** (stage2/stage3 skc IR byte-identical); new stage0 reports `skc 0.4.1 (ab4bad9)`; **full compiler test suite 1766/1766** against the new stage0. IR pushed to `SkipLabs/skiplang-bootstrap@2ef40fd` (`Update to ab4bad9`), fast-forwarded on its `main` exactly as `bump-bootstrap.yml` does. - With the fold restored, fresh caches (`make clean`, stage0 rebuilt from the new bootstrap IR): `make stage1` + `make test` **1767/1767** (includes the new FloatToString test), prelude **131/131**, skargo **15/15** (the 6 local failures seen with the stale system `skc` 0.3.3 on PATH pass with the freshly built skc), and the prelude runtime regression script passes ("Wasm Float.toString matches JS digits"). - CI builds stage0 from the submodule pointer in this PR, so the restored fold is exercised consistently (stage0 carries the new formatter; stage1 folds with it). ## Notes - After merging, republish the CI images (docker-publish) since the pinned bootstrap changed. - AI assistance: commits generated with Mistral Vibe (Co-Authored-By trailers); validation performed in-session as described above.
mbouaziz
added a commit
to SkipLabs/skjs
that referenced
this pull request
Nov 17, 2026
tsc keys number literal types by their double. SKJS normalized literals exactly only within double precision and kept beyond-precision spellings verbatim, so 9007199254740993 and 9007199254740992 (both 2^53), 0.300000000000000044 and 0.30000000000000004, or 1e-400 and 0 were distinct literal types. normalizeJSNumber now names such a literal by the ECMAScript String() form of the double it denotes (canonicalizeBeyondPrecision): the digits come from Float.toString, which since Skip std 0.5.0 (SkipLabs/skip#1288) is the shortest round-trip form, laid out by the existing formatCanonicalJSNumber. A name is only rewritten when it parses back to the exact same double, so an older formatter (fewer digits) or an unparseable name keeps the spelled form: no two doubles can share a name. The fast path now requires a precise literal, since a canonical-shaped 16+ digit name can still denote a rounded double. Checked against node String(Number(x)) on 1500 random literals (long decimals, long fractions, out-of-range exponents, shortest forms of random doubles): all finite values match exactly. Values that overflow to Infinity keep their spelled form ("Infinity" is not a number literal name): 1e309 vs 2e309 stays a FIXME. Equality narrowing and truthiness still compare such names by value (previous commit). Flips the pinned assignability cases, updates the printed forms and the enum reverse-mapping key of a spelled radix member (now its real runtime key), replaces the unit test that pinned the spelled form, and updates skjs-rules.md rule 30. Version 1.23.0 (behavior change). Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
mbouaziz
added a commit
to SkipLabs/skjs
that referenced
this pull request
Nov 19, 2026
…carries a Float (#1077) Simplifies the number-literal machinery now that the Skip stdlib provides Ryu-based `Float.toString` (shortest round-trip, SkipLabs/skip#1288) and `Math.fmod` (SkipLabs/skip#1711): - `jsNumberToString` / `shortestFloatDigits` are now total: Ryu's shortest digits always read back to the same double, so the round-trip guard is gone - Enum constant folding carries doubles end-to-end: `EFNum(Float)` instead of `EFNum(String)`; the literal name is produced by `jsNumberToString` only when a member value is materialized - JS `%` over doubles folds with `Math.fmod` (exactly the truncated remainder with the dividend's sign, NaN cases included), replacing a hand-rolled Sterbenz loop; `enumDoubleInt32Base` uses it too No behavior change: full test suite passes (230 tests), and the checker binary produces identical results to the previous one on positive/negative enum-folding and literal-naming repros. New pins: `jsNumberToString` unit test and enum remainder cases (`5.5 % 2 = 1.5`, `-5.5 % 2 = -1.5`, `1e309 % 2 = NaN -> number`), verified against `tsc --strict`. Version bumped to 1.26.3 per the every-PR rule. Generated by Mistral Vibe (co-authored with the commit via `Co-Authored-By: Mistral Vibe <vibe@mistral.ai>`).
mbouaziz
added a commit
to SkipLabs/skjs
that referenced
this pull request
Nov 19, 2026
…x parsing - isJSNumericString is now a round-trip check (parse, then print back and compare) instead of a hand-rolled grammar: with Ryu-based Float.toString (std 0.5.0, SkipLabs/skip#1288) and the correctly-rounded runtime parser, 's is canonical iff s parses to a finite double that prints as s' — the same as tsc's numeric-name test (+s).toString() === s. The ~150 lines of iterator-based decimal/exponent scanning, isDigitChar and isPreciseJSNumberLiteral go away, along with normalizeJSNumber's fast path and the isJSNumberLiteralStringForm alias (isExactJSNumberName covers it) - jsNumberLiteralDouble is now total: radixToDecimalString returns None on invalid digits instead of throwing in Chars.hexDigitToInt, fixing a crash when probing arbitrary property-name strings (keyof { "0x1p3": ... }) - enumDoubleInt32Base is just Math.fmod(f, 2^32) for every finite double (exact at any magnitude), and shift counts share enumShiftCount Behavior notes: the round-trip check also rejects spellings the old grammar accepted but that are not real canonical forms, matching JS/tsc exactly: "9e+308" (String is "Infinity") and "9e-324" (String is "1e-323"). Test pins updated accordingly; full suite green (230 tests). Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
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.
What
Replace the pure-Skip
Float.toString, which printed at most 9 significantdigits and so could not round-trip, with a C helper built on Ryu
d2s(vendoredin #1287). It produces the shortest decimal string that round-trips through the
IEEE-754 double:
toString().toFloat() == valuefor every finite value, e.g.0.1prints"0.1"and0.1 + 0.2prints"0.30000000000000004".The layout is unchanged: fixed notation for 1e-4 <= value < 1e15,
.0forintegers, exponents
e+N,e-0Nande-NN. Only the number of digits changes,e.g.
DBL_MAXprints1.7976931348623157e+308instead of1.797693135e+308.JS compatibility: the digits are those of JS
Number.prototype.toString.This fixes skdb's JS API, which passes
FLOATvalues to JS asFloat.toString()text andparseFloats them, so values lost everything pastthe 9th significant digit.
How
SKIP_Float_toStringShortest(string.c) lays out Ryu's shortest digits inSkip's conventions;
floatToStringkeeps handlingnan,infand the sign.skcno longer constant-foldsFloat.toString(): folding uses thecompiler's own
Float.toString, i.e. that of the prelude the compiler wasbuilt with, so a constant could print differently from the same value at
runtime.
tests/Float.skexpectations updated to full precision, andpreviously non-round-tripping values (
5e-324, smallest normal,DBL_MAX,2^53, 2^53 - 1) added to the round-trip test. The TOML golden for long pi now
has all its digits. A new runtime regression
(
runtime/tests/float_to_string.{c,mjs}) builds the formatter for Wasm andchecks it against JS for 200k values.
std0.4.0 → 0.5.0 (visible output change),skc0.4.0 → 0.4.1.Testing Float.toString with any compiler
skcfoldstoString()on a Float literal at compile time with its ownFloat.toString, i.e. that of the prelude the compiler was built with. CI'scandidate toolchain is built from the bootstrap IR, which still has the 9-digit
formatter, so literal-based tests saw
5e-324→"4.940656458e-324"whateverthe runtime does. The
Float.toStringtests now parse their inputs withString.toFloat(never folded), so they test the runtime formatter with anycompiler; with the previous formatter they fail as expected. Literal folding
goes away for good once the bootstrap is bumped past this PR.
test_vtable_slotscompared function text including debug metadata numbers(
!dbg !N), which are per LLVM module. With this PR's smaller prelude, theedited function lands in the same module as the checked one and renumbers it,
while the vtable slot is unchanged. The test now strips
!dbgattachments(with the existing
llvm_artifact_diff.without_dbg_attachments) and stillfails if the slot offset changes.
Validation
Rebased onto
maine46671a9(the #1287 commits dropped; one conflict inruntime/tests/README.mdwith the new macOS section, both kept). The versionbumps are on top of #1683, which already moved
stdandskcto 0.4.0.round-trip failures, 0 layout differences from the old conventions, output
at most 23 characters. The later simplification of the formatter (109 → 47
lines) produces byte-identical output on 18.6M values.
prelude131/131,toml332/332, compilerRuntime/Native/Syntaxtests 378/378 (allFloat*tests), skdb nativeSQL suite against sqlite3 437/437,
runtime/tests/run.shpass.prelude131/131; with the previous formatter, the Float tests fail.test_vtable_slots, also passing onmain).skdbWasm build: +27 KB (Ryu'sd2stables).AI assistance
The original commits were written with Claude Code (Claude Opus 4.6). The
rebase, review and the commits on top (rename, cleanup, tests, version bumps)
were done
with Mistral Vibe (Claude Opus 5.5, thinking: high); see the
Co-Authored-Bytrailers.