Skip to content

prelude: parse String.toFloat natively with Ryu s2d + locale-safe strtod fallback - #1714

Merged
mbouaziz merged 2 commits into
mainfrom
tofloat-native-s2d
Nov 19, 2026
Merged

mbouaziz merged 2 commits into
mainfrom
tofloat-native-s2d

Conversation

@mbouaziz

@mbouaziz mbouaziz commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

Unifies String.toFloat parsing across native and Wasm, and removes a
locale hazard on native:

  • Native now uses Ryu s2d as the primary path (previously libc atof),
    exactly like Wasm — correctly rounded and locale-independent.
  • Locale safety: atof/strtod honor the process locale (under
    de_DE.UTF-8, strtod("1.5") returns 1.0). The fallback now rewrites
    the input's . to localeconv()->decimal_point before calling strtod,
    so parsing is correct under any installed locale. (The grammar is
    validated in String.toFloatOption to be decimal-only, so . is the only
    separator that can appear.)
  • >17-nonzero-digit inputs (an upstream Ryu s2d limit) fall back to
    strtod on native and JS Number() on Wasm; both are correctly rounded
    for decimal input, so native and Wasm agree bit for bit.

Tests

  • New runtime/tests/to_float_raw_native.c, wired into run.sh before
    the Linux-only section so it runs on macOS too: s2d-path values, the
    >17-digit fallback, -0.0, overflow/underflow, and a real
    comma-decimal locale (de_DE.UTF-8 when installed). Verified locally on
    macOS: under de_DE, plain strtod("1.5") gives 1.0 while the runtime
    gives 1.5.
  • string_to_float.mjs (Wasm s2d harness): new accepted edge forms
    ("5.", "0.", "1e0", "10e-1", "-0.0e0") checked against JS
    Number(), plus more >17-digit rejection cases documenting the fallback
    trigger. Passed in the Linux container (100k random inputs included).
  • tests/Float.sk: grammar pins (toFloatOption rejects whitespace, hex
    0x1p4, Infinity/INF, leading +, .5, 1,5, 00, 1e+; accepts
    5., 0e0, 1E5, 10e-1), long-digit rounding
    ("0.1000000000000000000000000000001" -> 0.1,
    "123456789012345678" -> 123456789012345680.0), the subnormal boundary
    ("2.2250738585072011e-308" -> largest subnormal 2.2250738585072009e-308),
    and a toString round-trip.

Validation

  • prelude/runtime/tests/run.sh passes on macOS (native toFloat test incl.
    the real de_DE.UTF-8 locale branch) and in the Linux container (full
    suite incl. Wasm s2d vs Number() on 100k random inputs).
  • string.c compiles cleanly on both platforms (-Wall -Wextra -Werror).
  • make check-fmt failure on the first push was diagnosed and fixed in
    ae47d93b: tests/Float.sk needed one expectEq on a single line.
    Re-verified locally with the pinned tool versions — skfmt (stage0 binary),
    clang-format 20 on both .c files, prettier 3.4.1 on the .mjs,
    shellcheck on run.sh — all clean.
  • Full stage1 compiler build runs on macOS (gmake 4.4.1; the earlier local
    build failure was GNU make 3.81 not supporting grouped &: targets,
    causing two skargo runs to race on state.db); prelude suite with the
    new runtime is run against stage1 locally.

Notes

  • Rebased on latest main (which already carries the std 0.7.0 bump, so
    this PR no longer touches Skargo.toml).
  • Runtime-C-only change, so no bootstrap bump needed (the C runtime is
    compiled from source at every stage0 link). CI images republish (already
    pending from Bump bootstrap to ab4bad9c and restore the FloatToString peephole fold #1688) will carry it.
  • AI assistance: commits generated with Mistral Vibe (Co-Authored-By
    trailers); validation performed in-session as described above.

mbouaziz and others added 2 commits November 19, 2026 16:43
…tod fallback

Native used libc atof, which is locale-dependent (a ',' locale parses "1.5"
as 1) and a different code path from Wasm. Use Ryu's s2d as the primary path
on native too (correctly rounded, locale-independent, same as Wasm); inputs
s2d rejects (more than 17 non-zero digits, an upstream Ryu limit) go to
strtod after rewriting '.' to the locale's decimal point, so native and Wasm
agree bit for bit for the decimal-only grammar validated in
String.toFloatOption.

Tests: native runtime test to_float_raw_native.c (runs on all platforms via
run.sh, covers s2d path, >17-digit fallback, and a comma-decimal locale),
edge formats and fallback rejections in the Wasm s2d harness
(string_to_float.mjs), and grammar/rounding cases in tests/Float.sk.

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>
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
mbouaziz merged commit a0519d7 into main Nov 19, 2026
1 check was pending
@mbouaziz
mbouaziz deleted the tofloat-native-s2d branch November 19, 2026 16:43
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