Skip to content

Selling a whole {{T}} lot whose total doesn't divide evenly fails to balance: a lot keeps only its rounded per-unit cost #2425

Closed

Description

@robcohen

Description

A lot bought with a total cost that doesn't divide evenly across its units, and later sold whole, fails to balance:

2024-01-01 open Assets:B X "FIFO"
2024-01-01 open Assets:C

2024-01-02 * "buy"
  Assets:B  3 X {{500 USD}}
  Assets:C

2024-02-01 * "sell the whole lot"
  Assets:B  -3 X {}
  Assets:C  500 USD
x Transaction does not balance: residual -1E-26 USD

The lot cost 500 USD and is sold for 500 USD. But a lot stores only its per-unit cost, 500/3 rounded to Decimal's digits (166.6666666666666666666666667), so the exact total is gone as soon as the lot is added. The sale is then booked at 3 × 166.66…67, which is 500.0000000000000000000000001. The amounts are whole dollars, so they infer no tolerance, and the imbalance booking created is an error that the ledger's author cannot remove.

The purchase itself balances in rustledger, because a {{T}} augmentation keeps its total as PerUnitFromTotal for residual math (#1026). The total is lost only when the lot enters the inventory.

Beancount

Beancount 3.2.0 has the same bug, and worse:

bc_total.beancount:6: Transaction does not balance: (-0.0000000000000000000000001 USD)
     Assets:B   -3 X {166.6666666666666666666666667 USD, 2024-01-02}

It also interpolates the purchase's cash leg as -500.0000000000000000000000001 USD. A fix here would be a deliberate divergence, on the side of correctness.

Scope

#2417 fixed the case of pools (a whole AVERAGE or {*} pool sale). There the exact basis is the sum of the source lots' costs, and those are exact when typed. For a single {{T}} lot there is no such sum: the one number that could reproduce the total is the total itself, and the lot doesn't keep it.

A fix needs a lot to remember its total, for example an optional exact total on Cost, or a lot-level field that a whole-lot sale books as PerUnitFromTotal. That touches:

  • Cost / Position and every constructor;
  • the rkyv cache layout (a loader cache version bump);
  • the FFI and component cost types;
  • the meta.hash contract rustfava pins in its snapshots.

The same gap remains for pools after #2424, because a pool reduced once becomes a single lot at the rounded average. Selling a pool of 1 at 100 and 2 at 200 as -1 X {} then -2 X {}, even as two postings in one transaction, books 166.66…67 + 2 × 166.66…67 and fails against 500 USD by 1E-26. Only a sale of the whole pool in one posting is exact today.

BQL shows the same root cause. cost(position) is computed from a position, and a position holds only the per-unit cost. So even where booking now carries the exact total, the two figures split: for a 3 X {{500 USD}} buy, weight is 500 but cost(position) is 500.00000000000000000000000001. After #2424 the same happens for a whole-pool sale (weight -500, cost(position) -500.00…01), so sum(cost(position)) over an emptied account prints -0 USD instead of 0.

It should also say what happens:

  • after a partial sale, when the remaining units' share of the total isn't exact either;
  • when lots are merged or pooled (AVERAGE, {*}), which could then carry exact totals instead of re-summing.

Related

Activity

  1. added
    bug
    on Sep 25, 2026
  2. added a commit that references this issue on Sep 29, 2026
    5aa8ec2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions