Repository navigation
Make RoundingSpec robust to floating-point representation error - #159
Merged
Merged
Conversation
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>
Contributor
Shouldn't we make this target-value dependent? Very different when talking about 3m Euros of wealth vs bea of 15 Euros. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
approved these changes
Oct 8, 2026
Name the dtype of the example value in the changelog and the comment. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.
Directed rounding reacted to the representation error of decimal operands. A rate
truncated to
1e-6and multiplied by a whole-Euro amount lands one ulp below theexact product (
1e-6 * 65600 * 40000is2623.9999999999995), and a full-EuroRoundingSpec(direction="down")then dropped a whole Euro (2623 instead of 2624).The same happens upwards (
0.07 * 100is7.000000000000001, rounded up to 8).RoundingSpec.apply_roundingnow snaps the quotientout / baseto the nearestwhole 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,floororround, selected by a lookup ondirection.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 amillionth 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 of12289. 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 onboth backends. The rounding test compares with
rtol=1e-7, since the defaulttolerance 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, 30skipped.
🤖 Generated with Claude Code