Skip to content

fix(compat): compare balances as money, not as raw Decimals - #1916

Merged
robcohen merged 1 commit into
mainfrom
fix/compat-compare-as-money
Aug 1, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/compat-compare-as-money

Conversation

@robcohen

@robcohen robcohen commented Aug 1, 2026

Copy link
Copy Markdown
Member

Re-lands the correction from #1910. That PR auto-merged at 15:18 yesterday with head 8b2342e82; I pushed this commit afterwards, so it was stranded on the branch and never reached main. Same content, plus a comment correction.

What

The value oracle compared raw Decimals, which is the one question guaranteed to expose a representation difference rather than a value disagreement. Each side is now quantized to beancount's own exponent before comparing.

The two tools legitimately carry different precision for the same balance because they quantize at different layers: beancount rounds when it books; rledger books what the arithmetic implies and rounds when it displays.

0.035 SPY @ 137.142857143 USD against an explicit 4.80 leaves 5e-12. beancount books 0.00 and loses it. rledger books 0.000000000005, keeps the postings summing to exactly zero, and renders 0.03 on every surface that knows the currency. Only sum(number) — a deliberately currency-less projection, which is what this harness asks for — shows the residue, and there the exact sum is the correct answer.

That is why #1909 was closed as working-as-intended: rledger preserving the value is the better behavior, since beancount silently discards a tenth of a cent someone deliberately typed.

Quantizing to beancount's exponent is principled rather than a fudge: beancount has already rounded to the currency's precision at booking, so its exponent is the precision at which these figures are money.

Stated cost

A genuine divergence smaller than one display unit is now invisible. That is the deliberate trade, and it is written into the docstring — it is also, by definition, a difference no user could observe in a balance.

Verification

The harness was shown able to report dirty, not just clean:

case reported
#1909 repr: 0.03 vs 0.030000000005 silent
#1909 repr: 0.00 vs -0.0003183 silent
one cent wrong reported
sign flip reported
missing posting reported
magnitude blowup reported

Corpus, with #1913 also applied: 4 divergences to 1, the survivor being issue-520.beancount, a tracked regression fixture.

Also

The workflow comment listed #1911 as a known divergence. #1913 fixes it, so the comment now names only issue-520.

Re-lands the correction from #1910, which was pushed after that PR had
already auto-merged and so never reached main.

The comparison used raw Decimal equality, which reported two divergences
that were representation differences rather than value disagreements. The
two tools quantize at different layers: beancount rounds when it books,
rledger books what the arithmetic implies and rounds when it displays.

`0.035 SPY @ 137.142857143 USD` against an explicit `4.80` leaves 5e-12.
beancount books 0.00 and loses it; rledger books 0.000000000005, keeps
the postings summing to exactly zero, and renders 0.03 on every surface
that knows the currency. Only `sum(number)` - a deliberately
currency-less projection, which is what this harness asks for - shows the
residue, and there the exact sum is the correct answer. #1909 was closed
as working-as-intended on that basis: rledger preserving the value is the
better behavior, since beancount silently discards a tenth of a cent
someone deliberately typed.

Each side is now quantized to beancount's own exponent before comparing:
beancount has already rounded to the currency's precision at booking, so
its exponent IS the precision at which these figures are money. Stated
cost: a genuine divergence smaller than one display unit is invisible,
which is by definition a difference no user could observe in a balance.

Verified against seven cases that this silences only what it should -
both #1909 representation cases go quiet, while a one-cent error, a sign
flip, a missing posting and a magnitude blowup all still report.

Corpus, with #1913 also applied: 4 divergences -> 1, the survivor being
issue-520.beancount, a tracked regression fixture.
@robcohen
robcohen requested a review from a team as a code owner August 1, 2026 02:49
Copilot AI review requested due to automatic review settings August 1, 2026 02:49
@robcohen
robcohen enabled auto-merge (squash) August 1, 2026 02:49

Copilot AI 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.

Pull request overview

Updates the booked-value compatibility harness to treat compared balances as monetary quantities (quantized to beancount’s booked precision) rather than comparing raw Decimal representations, reducing false-positive divergences when the two tools retain different internal precision.

Changes:

  • Quantize rledger totals to beancount’s exponent before comparing, and treat un-quantizable cases as true divergences (scripts/compat-values.py).
  • Update the compat workflow step comment to reflect the current single known divergence (.github/workflows/compat.yml).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
scripts/compat-values.py Quantizes rledger values to beancount’s booked precision prior to comparison; expands rationale in docstring.
.github/workflows/compat.yml Comment-only update: refreshes the “known divergences” note for the value comparison step.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented Aug 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 93%
Full AST Match 100%
Total Files 812
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen merged commit bf7c988 into main Aug 1, 2026
40 of 42 checks passed
@robcohen
robcohen deleted the fix/compat-compare-as-money branch August 1, 2026 02:59
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