Skip to content

fix(codegen): float_to_string on core WASM, one format everywhere (#643) - #810

Merged
hyperpolymath merged 2 commits into
mainfrom
claude/issue-643-float-to-string
Nov 26, 2026
Merged

hyperpolymath merged 2 commits into
mainfrom
claude/issue-643-float-to-string

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Closes #643

What

float_to_string type-checked but failed core-WASM codegen with UnboundVariable. The interpreter's output (string_of_float: "3.", 12 digits) also disagreed with Bun's String(x) (shortest round-trip). All three backends now print the same text, using the ECMAScript Number::toString format.

  • lib/wasm_dtoa.ml: a shortest round-trip printer in raw WASM.
    • Digits come from Burger & Dybvig's free-format algorithm on 40-limb bignums in linear memory. It needs no tables and no host import.
    • Exact ties go to the even digit, as in Bun.
    • It is emitted once per module that uses it, appended after every other function so no existing index moves. Call sites are patched to it when the module is assembled.
    • Its scratch space sits above the heap pointer and is not kept, so a call holds only its result string.
  • lib/float_format.ml: the same format in OCaml. The interpreter's float_to_string uses it.
  • Modules that don't call float_to_string are byte-for-byte unchanged.

Behaviour change

The interpreter now prints 3.0 as "3" (was "3.") and keeps up to 17 significant digits (was 12). This also changes stdlib/json.affine's JFloat rendering in the interpreter, which now matches the Bun output.

Tests

  • tests/codegen/test_float_to_string.mjs checks 22,655 doubles against Bun's String(x). The cases are powers of 2 and 10 and their neighbouring doubles, subnormals, the extremes, exact ties, and seeded random bit patterns.
    • During development, an extra 1.8M random doubles and 107k boundary neighbours also matched.
  • test/test_float_format.ml pins the shared table, the interpreter, and the module assembly (printer appended, call patched, no placeholder left).
  • dune test: 623 tests pass. All 39 WASM harnesses pass. just test-faces, the soundness ledger and the governance gates pass.

Audit asked for in the issue

to_lowercase, to_uppercase, trim, string_sub and string_from_char_code all compile on core WASM. show does not compile for any type (show(1) fails too). It needs type-directed lowering and is left out of this PR.


Generated by Claude Code

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 55 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 1339db58-6dbb-4ca1-adac-8c56d1f61fc1
Commits

Reviewing files that changed from the base of the PR and between 29b527e and d491469.

Files selected for processing: 11

  • CHANGELOG.adoc
  • lib/codegen.ml
  • lib/dune
  • lib/float_format.ml
  • lib/interp.ml
  • lib/wasm_dtoa.ml
  • stdlib/string.affine
  • test/test_float_format.ml
  • test/test_main.ml
  • tests/codegen/float_to_string.affine
  • tests/codegen/test_float_to_string.mjs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@gitnotebooks

gitnotebooks Bot commented Nov 26, 2026

Copy link
Copy Markdown

HoTT-stuff and others added 2 commits November 26, 2026 03:25
float_to_string type-checked but failed core-WASM codegen with
UnboundVariable, and the interpreter (string_of_float: "3.", 12 digits)
disagreed with Bun (String(x): shortest round-trip).

- lib/wasm_dtoa.ml: a shortest round-trip printer in raw WASM. It runs
  Burger & Dybvig's free-format digit generation on 40-limb bignums in
  linear memory, with no tables and no host import, and uses the
  ECMAScript Number::toString layout. Exact ties go to the even digit.
  It is emitted once per module, appended after every other function so
  no index moves; call sites are patched to it at assembly. Scratch
  space above the heap pointer is not kept, so a call holds only its
  result string.
- lib/float_format.ml: the same format in OCaml. The interpreter's
  float_to_string uses it, so all three backends print the same text.
- Tests: tests/codegen/test_float_to_string.mjs checks 22,655 doubles
  (powers of 2 and 10 and their neighbours, subnormals, extremes, ties,
  seeded random bit patterns) against Bun's String(x).
  test/test_float_format.ml pins the table, the interpreter and the
  module assembly.

Closes #643

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw
267522186230338.625 is the double itself; the shorter literal relied on
rounding, which SonarCloud flags (S6534). Also Number.NaN (S7773).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw
@hyperpolymath
hyperpolymath force-pushed the claude/issue-643-float-to-string branch from d7ac866 to d491469 Compare November 26, 2026 03:25
@sonarqubecloud

Copy link
Copy Markdown

@hyperpolymath
hyperpolymath marked this pull request as ready for review November 26, 2026 03:35
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-26T03:35:31.056513Z
We couldn’t complete this review. Comment @codex review to try again.
d491469 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@hyperpolymath
hyperpolymath merged commit 8dcaee2 into main Nov 26, 2026
18 checks passed
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@hyperpolymath
hyperpolymath deleted the claude/issue-643-float-to-string branch November 26, 2026 03:35
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.

codegen(core-WASM): float_to_string raises UnboundVariable at compile (works in resolve/typecheck/interp/Deno)

3 participants