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
@@ -47,10 +47,13 @@ pub enum Leftover {
|
||||
/// government's part of a payment), BS-arap-3 (a document line's
|
||||
/// exact amount).
|
||||
/// INVARIANTS:
|
||||
/// 1. Some(s) is the amount type's `checked_mul` of whole and weight,
|
||||
/// then its checked division by total. For Decimal the product and
|
||||
/// quotient are exact while they fit 96 bits and scale 28, and are
|
||||
/// rounded at that edge (rust_decimal's checked operations, K-U10);
|
||||
/// 1. Some(s) is the amount type's `checked_mul_rounded` of whole and
|
||||
/// weight, then its checked division by total. For Decimal the
|
||||
/// product and quotient are exact while they fit 96 bits and scale
|
||||
/// 28, and are rounded at that edge (rust_decimal's checked
|
||||
/// operations: a share by a percent of 27 places is a rounded figure
|
||||
/// by construction, and is computed so, to the digit; K-U10 left
|
||||
/// this one product rounding, because the quotient rounds anyway);
|
||||
/// for a machine integer the division truncates toward zero. This is
|
||||
/// the computation the six call sites made before U3, to the digit.
|
||||
/// 2. None when total is zero, or when the product or quotient is not
|
||||
@@ -68,7 +71,7 @@ pub enum Leftover {
|
||||
/// be None.
|
||||
/// ```
|
||||
pub fn share_of<T: Amount>(whole: T, weight: T, total: T) -> Option<T> {
|
||||
whole.checked_mul(weight)?.checked_div(total)
|
||||
whole.checked_mul_rounded(weight)?.checked_div(total)
|
||||
}
|
||||
|
||||
/// The largest whole multiple of `step` not above `whole × weight ÷ total`,
|
||||
@@ -120,7 +123,7 @@ pub fn share_floor_with<T: Amount>(
|
||||
if total <= T::ZERO || step <= T::ZERO {
|
||||
return None;
|
||||
}
|
||||
let num = whole.checked_mul(weight)?;
|
||||
let num = whole.checked_mul_rounded(weight)?;
|
||||
let quotient = num.checked_div(total)?;
|
||||
let mut part = round(quotient)?;
|
||||
// The quotient may have rounded across the exact floor: take whole
|
||||
@@ -130,7 +133,7 @@ pub fn share_floor_with<T: Amount>(
|
||||
// K-U10, pinned in the bridge). Both directions are bounded so a
|
||||
// quotient rounded far across the floor refuses instead of hanging.
|
||||
let mut steps = 0;
|
||||
while part.checked_mul(total)? > num {
|
||||
while part.checked_mul_rounded(total)? > num {
|
||||
if steps == MAX_CORRECTION_STEPS {
|
||||
return None;
|
||||
}
|
||||
@@ -143,13 +146,13 @@ pub fn share_floor_with<T: Amount>(
|
||||
return None;
|
||||
}
|
||||
let next = part.checked_add(step)?;
|
||||
if next.checked_mul(total)? > num {
|
||||
if next.checked_mul_rounded(total)? > num {
|
||||
break;
|
||||
}
|
||||
part = next;
|
||||
steps += 1;
|
||||
}
|
||||
let remainder = num.checked_sub(part.checked_mul(total)?)?;
|
||||
let remainder = num.checked_sub(part.checked_mul_rounded(total)?)?;
|
||||
Some((part, remainder))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user