fix(core): foreign amounts and rates take at most 10 places (D-75)

The K-U1 review: foreign 0.99999999999999999999999999 (26 places) x rate
0.005 into USD booked 0.01, though the exact product
0.00499999999999999999999999995 is under half a cent. Decimal::checked_mul
rounds a product finer than 28 places to 28 before fx_booked rounds it to
the cent: two roundings, one cent off. A ledger in one money books the
exact product rounded once (BS-ledger-1, BS-ledger-14).

Owner decision D-75 (2026-11-08): a new line's foreign amount and its rate
take at most MAX_LINE_SCALE (10) decimal places each, as a base amount does
(B-28), so their product has at most 20 places, under Decimal's 28.
Ledger::add_line_fx refuses past it with ValueOutOfRange ("foreign amount
..." / "FX rate ..."; the variant for an amount that is not a journal
line's), trailing zeros not counted, before computing, and never on replay
(is_replaying; replay_add_line_fx books a journal's older line verbatim).

Tests: 11-place foreign amount and rate refused; ten and ten accepted with
hand-checked books; the review's input refused, not mis-booked; trailing
zeros are not places; replay keeps a 26-place foreign line in core, and a
store journal holding one opens with its figures.

fx_booked's spec block states the precondition (scales within 10 each, and
the mantissas' product within Decimal's 96 bits) and that the result is
exact under it, and documents Decimal::checked_mul's behaviours outside it
(rounding past scale 28 and past 96 bits; 1e-28 x 1e-28 underflowing to
zero); invariant 1 now says None when T's checked_mul cannot hold the
product. The bridge covers every D-75 scale (0..=10 each, products to 20
places) against an independent exact reference, shows 26 places is where the
reference and Decimal part (and the engine refuses), and pins an open edge
this bridge found: ten places each still allow a 20-place product past
~7.9e8 that outgrows 96 bits (1050000010.0000000001 x 9.9999999999 books
10500000099.90, exact 10500000099.89). B-28 and B-3 claims record the
enforcement and checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-08 18:08:12 -07:00
1 parent bf2fe200c2
commit c5a4894985
7 files changed
+394 -33

No files matched your search

+90
View File
@@ -743,6 +743,96 @@ fn disposal_proceeds_and_dates_are_bounded() {
.unwrap();
}
/// D-75 (owner, 2026-11-08): a new line's foreign amount and its rate take
/// at most ten decimal places each, as a base amount does (B-28), so the
/// product is exact (at most twenty places, under Decimal's 28) and the
/// booked amount is rounded once. Found by the K-U1 review: a 26-place
/// foreign amount times 0.005 books 0.01 though the exact product is under
/// half a cent, because Decimal rounded the product to 28 places first.
/// Replay is exempt (B-28): an older line keeps its figures.
#[test]
fn a_foreign_amount_and_a_rate_take_at_most_ten_places() {
let dec = |s: &str| s.parse::<Decimal>().unwrap();
let mut b = books();
let e = b.ledger.try_create_entry(d(2026, 1, 1), "fx").unwrap();
let mut fx = |amount: &str, rate: &str| {
b.ledger.add_line_fx(
e,
b.cash,
Side::Debit,
Currency::EUR,
dec(amount),
dec(rate),
)
};
// Eleven places in the foreign amount, or in the rate: refused, naming
// which; ten and ten accepted.
assert_eq!(
fx("1.00000000001", "1"),
Err(LedgerError::ValueOutOfRange(
"foreign amount 1.00000000001".into()
))
);
assert_eq!(
fx("1", "1.00000000001"),
Err(LedgerError::ValueOutOfRange("FX rate 1.00000000001".into()))
);
// The review's input: refused, not mis-booked as 0.01.
assert_eq!(
fx("0.99999999999999999999999999", "0.005"),
Err(LedgerError::ValueOutOfRange(
"foreign amount 0.99999999999999999999999999".into()
))
);
// Ten places each: the product (twenty places) is exact and booked
// once. 0.0000000001 x 5000000000.0000000001 = 0.50000000000000000001
// books 0.50; 0.9999999999 x 0.5000000001 = 0.50000000004999999999
// books 0.50.
assert_eq!(fx("0.0000000001", "5000000000.0000000001"), Ok(()));
assert_eq!(fx("0.9999999999", "0.5000000001"), Ok(()));
// Trailing zeros are not places: 1.50000000000 is 1.5.
assert_eq!(fx("1.50000000000", "2.0000000000000"), Ok(()));
let lines = b.ledger.get_entry(e).unwrap().lines();
assert_eq!(lines.len(), 3, "the refused lines were not added");
assert_eq!(lines[0].amount.amount, dec("0.50"));
assert_eq!(lines[0].foreign_amount, Some(dec("0.0000000001")));
assert_eq!(lines[1].amount.amount, dec("0.50"));
assert_eq!(lines[2].amount.amount, dec("3.00"));
// The exact tie just under half a cent is not rounded up: with ten
// places each the product is held, so 0.4999999999 x 0.01 = 0.004999999999
// books to zero and is refused as before.
assert_eq!(
b.ledger.add_line_fx(
e,
b.cash,
Side::Debit,
Currency::EUR,
dec("0.4999999999"),
dec("0.01")
),
Err(LedgerError::InvalidFxRate(dec("0.01")))
);
// Replay keeps an older line with a finer foreign amount, exactly as
// booked.
b.ledger
.replay_add_line_fx(
e,
b.cash,
Side::Debit,
usd(dec("0.01")),
Currency::EUR,
dec("0.99999999999999999999999999"),
)
.unwrap();
let lines = b.ledger.get_entry(e).unwrap().lines();
assert_eq!(lines.len(), 4);
assert_eq!(
lines[3].foreign_amount,
Some(dec("0.99999999999999999999999999"))
);
assert_eq!(lines[3].amount.amount, dec("0.01"));
}
/// The bounds are the line limit exactly: a new draft line (base or
/// foreign) and a recurring template's line may be as large as a line, and
/// no larger; a replayed one past it is accepted as journaled.