Skip to content

Make RoundingSpec robust to floating-point representation error - #159

Merged
MImmesberger merged 7 commits into
mainfrom
rounding-representation-error
Oct 8, 2026
Merged

MImmesberger merged 7 commits into
mainfrom
rounding-representation-error

Conversation

@MImmesberger

@MImmesberger MImmesberger commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Directed rounding reacted to the representation error of decimal operands. A rate
truncated to 1e-6 and multiplied by a whole-Euro amount lands one ulp below the
exact product (1e-6 * 65600 * 40000 is 2623.9999999999995), and a full-Euro
RoundingSpec(direction="down") then dropped a whole Euro (2623 instead of 2624).
The same happens upwards (0.07 * 100 is 7.000000000000001, rounded up to 8).

RoundingSpec.apply_rounding now snaps the quotient out / base to the nearest
whole number when it lies within 4 ulps of one, with the tolerance scaled by the
quotient's magnitude and the dtype's machine epsilon, and capped at a tenth of a
base unit. Floating-point error is relative, so an absolute cutoff would stop
working for amounts in the millions; scaling by the dtype's epsilon keeps the rule
meaningful under float32 (the JAX default), where a decimal rounding of the quotient
loses its integer part. The snapped quotient then goes through ceil, floor or
round, selected by a lookup on direction.

The tolerance is small and capped because in float32 a few ulps are a visible part
of a base unit: 16 ulps of 13062 / 72000 / 1e-6 (181416.66) span 0.35 of a
millionth and would truncate the rate to 0.181417 instead of 0.181416, and 16 ulps
of 0.204833 * 60000 (12289.98) span 0.023 and would floor it to 12290 instead of
12289. Both are rows of the LfSt Bayern cases in gettsim#1237 under the JAX backend.
A product or quotient of two decimal operands lands within about one ulp of its
exact value, so 4 ulps keep a margin while leaving such fractions alone.

Surfaced in ttsim-dev/gettsim#1237, where six Progressionsvorbehalt cases from the
LfSt Bayern calculator fail by 1 € on exact-Euro products.

Tests: six new witnesses in tests/tt/test_rounding.py (down at 2.6e3 and 2e8, up,
base 1e-6, and the two float32 rows above), red before the fix and green after on
both backends. The rounding test compares with rtol=1e-7, since the default
tolerance hides a result that is one base unit off for large quotients. At the
current head: tests: 1737 passed, 58 skipped. tests-jax: 1765 passed, 30
skipped.

🤖 Generated with Claude Code

MImmesberger and others added 2 commits October 7, 2026 18:38
Directed rounding floored 0.0656 * 40000 to 2623 because the binary product
is 2623.9999999999995. The quotient out / base is rounded to nine decimals
before ceil, floor or round, so a value within 1e-9 of a whole base counts as
that whole base.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@hmgaudecker

Copy link
Copy Markdown
Contributor

so a value within 1e-9 of a whole base counts as that whole base. Values that are genuinely off a whole base by more than 1e-9 are unaffected.

Shouldn't we make this target-value dependent? Very different when talking about 3m Euros of wealth vs bea of 15 Euros.

@codecov

codecov Bot commented Oct 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

MImmesberger and others added 3 commits October 8, 2026 12:22
An absolute tolerance on the quotient stops catching representation error
once amounts reach the millions, and rounding to nine decimals loses the
integer part in float32. The quotient now counts as a whole base when it is
within 16 ulps of one, scaled by its magnitude and the dtype's epsilon.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
MImmesberger added a commit to ttsim-dev/gettsim that referenced this pull request Oct 8, 2026
Drop the sonstige_leistungen_y and aufstockungsbeträge_altersteilzeit_y
inputs. Apply the full-Euro truncation of the tariff on the
Steuersatzeinkommen via RoundingSpec instead of floor calls, and drop the
millionths trick in the besonderer Steuersatz product. Rewrite the
docstrings of the transfer income functions.

Six LfSt Bayern cases fail by one Euro until ttsim-dev/ttsim#159 is
released and the pin is bumped.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
In float32, 16 ulps of a quotient near 2e5 span a third of a base unit
and 16 ulps near 1.2e4 span 0.023 of one, so genuine fractions were
snapped to the next whole base. Representation error of a product or
quotient of two decimal operands is about one ulp, so 4 ulps keep a
margin while leaving genuine fractions alone. The cap bounds the snap
where a few ulps are a sizeable part of a base unit.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@hmgaudecker hmgaudecker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks!

Comment thread CHANGES.md Resolved
Comment thread src/ttsim/tt/rounding.py Resolved
Comment thread src/ttsim/tt/rounding.py Resolved
Name the dtype of the example value in the changelog and the comment.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@MImmesberger
MImmesberger merged commit f1c6e05 into main Oct 8, 2026
13 checks passed
@MImmesberger
MImmesberger deleted the rounding-representation-error branch October 8, 2026 14:27
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