Skip to content

Preserve float precision in RFC 8785 number formatting - #1099

Merged
byroot merged 1 commit into
ruby:masterfrom
ydah:fix-rfc8785-number-precision
Oct 7, 2026
Merged

byroot merged 1 commit into
ruby:masterfrom
ydah:fix-rfc8785-number-precision

Conversation

@ydah

@ydah ydah commented Oct 7, 2026

Copy link
Copy Markdown
Member

RFC 8785 §3.2.2.3 requires ECMAScript number serialization. The current "%.15E" formatting limits numbers to 16 significant digits and can change the underlying double value.

0.1 + 0.2 is not an RFC test vector, but ECMAScript serializes its result as 0.30000000000000004. Float::MAX is covered by Appendix B.

Before

JSON.generate(0.1 + 0.2, rfc8785: true)
# => "0.3"

JSON.generate(Float::MAX, rfc8785: true)
# => "1.797693134862316e+308"

After

JSON.generate(0.1 + 0.2, rfc8785: true)
# => "0.30000000000000004"

JSON.generate(Float::MAX, rfc8785: true)
# => "1.7976931348623157e+308"

This replaces "%.15E" with Float#to_s while retaining ECMAScript notation formatting. On CRuby, all Appendix B vectors pass, and 10,000 random finite doubles match Node.js and round-trip correctly.

The original implementation also has an open PR addressing the same issue: dryruby/json-canonicalization#6.

@byroot
byroot merged commit 193df55 into ruby:master Oct 7, 2026
42 checks passed
@ydah
ydah deleted the fix-rfc8785-number-precision branch October 7, 2026 13:08
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.

2 participants