Add decimal rounding cases that distinguish decimal from binary rounding - #114
Open
micahhausler wants to merge 1 commit into
Open
Add decimal rounding cases that distinguish decimal from binary rounding#114micahhausler wants to merge 1 commit into
micahhausler wants to merge 1 commit into
Conversation
RFC 9651 Section 4.1.5 specifies rounding on the decimal value: ties go to the even multiple of one thousandth. "Equidistant" and "even" are statements about base-10 digits, so a binary double cannot represent the values the rule describes. Every rounding input in the suite today (0.0015, 0.0025, 9.9995) happens to scale to an exact midpoint as a double, so scaling by 1000 and rounding ties-to-even reproduces the specified result. Inputs whose binary intermediate lands just off the midpoint do not, and the suite contained no such case. 0.5015 is stored as 0.50149999999999994582..., so 0.5015 * 1000 is 501.49999999999994. That is below the midpoint rather than on it, and rounds to 0.501 where the specification requires 0.502. The error runs both ways: 2.0005 is stored above its decimal value, scales to 2000.5000000000002, and rounds to 2.001 where the specification requires 2.0. Divergence is confined to exact decimal ties; a non-tie has enough slack that binary error cannot cross a rounding boundary. Four cases are added. 0.5015 and 2.0005 are the revealing cases because they diverge in opposite directions, -0.5015 covers the sign path, and 0.5035 is a second positive witness against special-casing a single value. 2.0005 also reaches step 8 of Section 4.1.5, which appends a single "0" for a zero fractional component. Implementations should keep the rounding decision out of binary floating point. One holding a decimal type, or rounding the field value text, has nothing to do. One whose API accepts a float should interpret it as the shortest decimal string that round-trips to it, which is the default float formatting in Rust, Python, JavaScript, Go and Java, then round those digits with integer arithmetic. Harnesses must convert expected through that text form. Python's Decimal(float) and Java's new BigDecimal(double) capture the exact binary value and will fail a correct implementation.
Member
|
Thanks! These cases depend on the harness reading |
Contributor
Won't this require harnesses to implement a custom JSON parser? Should we use something like |
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.
Resolves #113