Skip to content

Add decimal rounding cases that distinguish decimal from binary rounding - #114

Open
micahhausler wants to merge 1 commit into
httpwg:mainfrom
micahhausler:decimal-rounding
Open

Add decimal rounding cases that distinguish decimal from binary rounding#114
micahhausler wants to merge 1 commit into
httpwg:mainfrom
micahhausler:decimal-rounding

Conversation

@micahhausler

Copy link
Copy Markdown

Resolves #113

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.
@mnot

mnot commented Aug 18, 2026

Copy link
Copy Markdown
Member

Thanks!

These cases depend on the harness reading 0.5015 as decimal text rather than converting the JSON number through a double. That convention isn't written down -- the existing 0.0025 and 9.9995 cases already rely on it too. Please add it to the README as part of this PR.

@apasel422

Copy link
Copy Markdown
Contributor

These cases depend on the harness reading 0.5015 as decimal text rather than converting the JSON number through a double. That convention isn't written down -- the existing 0.0025 and 9.9995 cases already rely on it too.

Won't this require harnesses to implement a custom JSON parser? Should we use something like {"__type": "decimal", "value": "0.0025"} instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Add decimal rounding cases that distinguish decimal from binary rounding

3 participants