Fix #24: render non-integer floats in shortest round-trippable form#25
Merged
Conversation
LuaJIT's tostring / %g defaults to 14 significant digits, so numToStr (and the debug printer to_str) printed (+ 0.1 0.2) as "0.3" instead of the actual double "0.30000000000000004" — a lossy, non-round-tripping form that diverged from shen-cl/shen-rust/shen-go/ShenScript. Add a shortest_float helper (runtime.lua) that emits the smallest %.<p>g (p = 1..17) whose tonumber() parses back to exactly the same double, and route both numToStr (the str/print path) and to_str (the REPL/debug printer) through it. Integer-valued floats still print bare via the existing %d branch; kernel/ klambda untouched. Verified: (str (+ 0.1 0.2)) => 0.30000000000000004, (str (/ 10 4)) => 2.5, (str (+ 3.5 1.25)) => 4.75. make test 448 pass / 0 fail; make certify 100%. bifrost float cases (int-div-to-float, float-add-clean, float-add-imprecise) now agree across all five impls. Co-Authored-By: Claude Opus 4.8 (1M context) <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 #24.
Non-integer floats were printed via LuaJIT's
tostring(%.14g), which is lossy and not the shortest round-trippable form:(+ 0.1 0.2)printed0.3instead of the actual double0.30000000000000004, diverging from shen-cl / shen-rust / shen-go / ShenScript.Fix: a
shortest_floathelper inruntime.luaemits the smallest%.<p>g(p = 1..17) whosetonumber()parses back to exactly the same double. Both the canonicalstr/print path (numToStrinprims.lua) and the REPL/debug printer (to_strinruntime.lua) route through it. Integer-valued floats still print bare via the existing%dbranch; kernel/klambdauntouched.Verification:
make test448 pass / 0 fail (incl. new#24regression assertions inprimitives_spec.lua);make certify134/134 (100%).This is the last of the three
float-formattingdivergences tracked by bifrost — with this + pyrex41/shen-go#11, all five impls now agree, and bifrost's float cases are promoted from documented divergences to hard agreement assertions (pyrex41/bifrost#1 /verify/converged-divergences). Verified end-to-end: a combined tree (this fix + the#22hush fix) runs bifrost fully green (30 pass, 0 diverge, 0 FAIL).🤖 Generated with Claude Code