fix: canonicalize double rendering#999
Open
He-Pin wants to merge 1 commit into
Open
Conversation
4bee239 to
30e0ecc
Compare
Contributor
Author
|
Scala.js has different Double.tostring, fixing |
659f40a to
457a0a9
Compare
457a0a9 to
eb9397f
Compare
Motivation: Double rendering was split across renderer implementations and depended on platform defaults. JVM emitted uppercase scientific notation, Scala.js/Wasm used different fixed/scientific thresholds, and large whole doubles could expose exact binary64 noise in some manifestation paths. Modification: Route renderers through RenderUtils.renderDouble. Canonicalize double strings by keeping common JSON-range values in fixed decimal form, normalizing smaller scientific notation, and rendering out-of-Long-range whole doubles from BigDecimal.valueOf instead of the exact binary constructor. Keep TOML in sync and update directional/golden tests. Document two design choices that otherwise read as arbitrary: - The [1e-6, 1e21) fixed-decimal window is ECMAScript's Number.prototype.toString convention, which JSON/Java/Scala.js all follow. - HALF_EVEN on BigDecimal.valueOf(d) is equivalent to any other tie-breaking rule in this branch, because doubles that reach it have |d| >= 2^53 where ULP >= 1 and no representable value is an exact .5 tie. Result: sjsonnet now renders representative doubles consistently across JVM, JS, Wasm, and Graal without claiming Go/C++ exact-digit compatibility. - 0.000001 -> 0.000001 - 0.0000001 -> 1e-07 - 1e100 -> 1 followed by 100 zeros (no binary64 noise) Tests: - scalafmt on changed files - ./mill __.checkFormat - ./mill sjsonnet.jvm[2.13.18].test (all pass) - ./mill sjsonnet.js[2.13.18].test (all pass) - ./mill sjsonnet.wasm[2.13.18].test (all pass) - sjsonnet/test/graalvm/run_test_suites.py References: None - double rendering canonicalization.
eb9397f to
b1bdb3b
Compare
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.
Motivation
Double rendering was split across five renderer implementations
(
BaseByteRenderer,BaseCharRenderer,BaseRenderer,ByteRenderer,Renderer+TomlRenderer), depended on platform defaults (JVM emitsuppercase
1.0E-6, Scala.js/Wasm emit lowercase1e-7), and large wholedoubles exposed exact binary64 noise via
new BigDecimal(d).Modification
RenderUtils.renderDouble.ewith explicit signand 2-digit exponent padding.
BigDecimal.valueOf(d)instead ofnew BigDecimal(d)forout-of-Long whole doubles, eliminating binary64 noise.
TomlRendererwith the shared helper.Number.prototype.toStringconvention for the[1e-6, 1e21)fixed-decimal window.Result
Stable, canonical double rendering across JVM / JS / Wasm / Graal:
0.000001->0.0000010.0000001->1e-071e100->1+ 100 zerosAll tests pass on JVM/JS/Wasm/Native/Graal.
References
None — double rendering canonicalization.