fix(core): Decimal sums and products are exact or refused
rust_decimal's checked_add and checked_mul return None only when the integer part overflows. When the exact result needs more than 96 bits of mantissa, or a scale past 28, they round it and return Some: MAX + 0.4 is Some(MAX) (K-U4), 0.01 + 792281625142643375935439503.35 is ...503.4 (K-U8), 1050000010.0000000001 x 9.9999999999 is 10500000099.895 where the exact product is 10500000099.89499999999999999999 (K-U1), and 123456789.0123456789 squared loses its last digits (K-U3). The kernel's contract is exact or None, and BS-ledger-1 says a product the arithmetic cannot hold MUST be refused. impl Amount for Decimal now runs rust_decimal's operation and checks, in 256-bit integers (kernel/decimal_exact.rs), that the figure equals the exact sum or product as a rational; otherwise None. It builds no rounded figure of its own, and an exact result is returned as rust_decimal gave it, scale and all, so nothing exact moves. Comparing values is complete where the K-U10 row's "result scale fell below the operands'" test is not: 100 x 0.3333333333333333333333333333 is exact with a full-scale mantissa past 96 bits (trailing zeros), and percent_of computes it for every ratio. checked_sub is rust_decimal's subtraction under the same check (the trait default gave -0 for 0 - 0). Money::checked_add and checked_sub go through Amount; sum_exact*, add_exact and sub_exact already did. Two products could not simply become exact, and the suite found both: - Shares multiply a cent amount by a many-place percent (33.333333333333333333333333333, 27 places) and then divide, which rounds anyway (spec_cash_flow's government payment). They use the new Amount::checked_mul_rounded, rust_decimal's own product, so every share is the figure it always was. - A foreign amount at a rate (fx_booked) rounds once, from the exact product: Amount::checked_mul_round_to_step holds it in 256 bits, cuts it only as far as Decimal needs with a sticky digit for what was cut, and rounds with round_step. The one posted-range figure that moves is the one the old double rounding got wrong: 1050000010.0000000001 x 9.9999999999 books 10500000099.89, the exact product's cent, not 10500000099.90. checked_div and checked_div_exact are unchanged. Inherent Decimal::checked_* calls outside the kernel are listed in docs/proofs/k-u10.md and not converted. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4da1c5b30f
commit
38da08b2c2
7 files changed
+585
-72
No files matched your search
@@ -69,6 +69,7 @@
|
||||
|
||||
mod allocate;
|
||||
mod control;
|
||||
mod decimal_exact;
|
||||
mod entries;
|
||||
mod fx;
|
||||
mod payroll;
|
||||
|
||||
Reference in new issue
Block a user