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
@@ -12,7 +12,8 @@ use rust_decimal::Decimal;
|
||||
pub trait Amount: Copy + Ord {
|
||||
/// Zero.
|
||||
const ZERO: Self;
|
||||
/// `self + rhs`, or `None` when it cannot be represented exactly.
|
||||
/// `self + rhs`, or `None` when it cannot be represented exactly (for
|
||||
/// [`Decimal`], never a sum `rust_decimal` had to round: K-U10).
|
||||
fn checked_add(self, rhs: Self) -> Option<Self>;
|
||||
/// `-self`, or `None` when it cannot be represented.
|
||||
fn checked_neg(self) -> Option<Self>;
|
||||
@@ -27,9 +28,40 @@ pub trait Amount: Copy + Ord {
|
||||
fn from_u32(n: u32) -> Option<Self>;
|
||||
/// `self * rhs`, or `None` when it cannot be represented exactly.
|
||||
///
|
||||
/// A quantity times a unit price (a document line, BS-arap-3) is the one
|
||||
/// use: it never rounds.
|
||||
/// A quantity times a unit price (a document line, BS-arap-3), a
|
||||
/// category's book amount times its percent, and the other products
|
||||
/// that are the amount itself: it never rounds. For [`Decimal`] that
|
||||
/// is a check, not a given (K-U10): `rust_decimal` rounds a product
|
||||
/// whose mantissa passes 96 bits or whose scale passes 28, and this
|
||||
/// refuses it instead.
|
||||
fn checked_mul(self, rhs: Self) -> Option<Self>;
|
||||
/// `self * rhs` as the amount type multiplies, which for [`Decimal`] is
|
||||
/// `rust_decimal`'s: exact where the product fits 96 bits and scale
|
||||
/// 28, otherwise rounded to what it holds. `None` only on overflow.
|
||||
///
|
||||
/// This is the engine's rounding product, as [`Amount::checked_div`]
|
||||
/// is its rounding division, and for the same use: a proportional
|
||||
/// share (`whole x weight / total`, [`crate::kernel::share_of`]) whose
|
||||
/// weight is a percent or a ratio of many places (33.333...3, twenty
|
||||
/// seven places) is a rounded figure by construction, and has always
|
||||
/// been computed this way, to the digit. Nothing else calls it; a
|
||||
/// product that is the amount is [`Amount::checked_mul`]. A machine
|
||||
/// integer never rounds, so there it is `checked_mul`.
|
||||
fn checked_mul_rounded(self, rhs: Self) -> Option<Self> {
|
||||
self.checked_mul(rhs)
|
||||
}
|
||||
/// `self * rhs`, computed exactly and rounded once to a whole multiple
|
||||
/// of `step` in `mode`; `None` when `step` is not positive or the
|
||||
/// result cannot be represented.
|
||||
///
|
||||
/// The one use is a foreign amount at a rate ([`crate::kernel::fx_booked`]).
|
||||
/// For [`Decimal`] the exact product may need more than 96 bits (ten
|
||||
/// places each, D-75, is twenty places, and past 7.9e8 the mantissa
|
||||
/// passes `Decimal`'s): it is held wider than `Decimal` for the one
|
||||
/// rounding, never rounded early and then again.
|
||||
fn checked_mul_round_to_step(self, rhs: Self, step: Self, mode: Rounding) -> Option<Self> {
|
||||
super::round::round_step(self.checked_mul(rhs)?, step, mode)
|
||||
}
|
||||
/// `self / rhs` exactly, or `None` when `rhs` is zero or the quotient
|
||||
/// is not a whole amount of `Self` (never a rounded share).
|
||||
fn checked_div_exact(self, rhs: Self) -> Option<Self>;
|
||||
@@ -79,8 +111,16 @@ pub const MAX_LINE_SCALE: u32 = 10;
|
||||
|
||||
impl Amount for Decimal {
|
||||
const ZERO: Self = Decimal::ZERO;
|
||||
// Exact or `None` (K-U10): `rust_decimal`'s own add and multiply round a
|
||||
// result that needs more than 96 bits (or a scale past 28) instead of
|
||||
// refusing it; `decimal_exact` checks the figure is the exact one.
|
||||
fn checked_add(self, rhs: Self) -> Option<Self> {
|
||||
Decimal::checked_add(self, rhs)
|
||||
super::decimal_exact::exact_add(self, rhs)
|
||||
}
|
||||
// `rust_decimal`'s own subtraction, checked the same way (it is not
|
||||
// `self + (-rhs)` for a zero: that gives a negative zero).
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
super::decimal_exact::exact_sub(self, rhs)
|
||||
}
|
||||
fn checked_neg(self) -> Option<Self> {
|
||||
Some(-self)
|
||||
@@ -92,8 +132,23 @@ impl Amount for Decimal {
|
||||
Some(Decimal::from(n))
|
||||
}
|
||||
fn checked_mul(self, rhs: Self) -> Option<Self> {
|
||||
super::decimal_exact::exact_mul(self, rhs)
|
||||
}
|
||||
fn checked_mul_rounded(self, rhs: Self) -> Option<Self> {
|
||||
Decimal::checked_mul(self, rhs)
|
||||
}
|
||||
fn checked_mul_round_to_step(self, rhs: Self, step: Self, mode: Rounding) -> Option<Self> {
|
||||
if let Some(product) = super::decimal_exact::exact_mul(self, rhs) {
|
||||
return super::round::round_step(product, step, mode);
|
||||
}
|
||||
// The exact product does not fit `Decimal`: round it once, from a
|
||||
// wider integer, not twice.
|
||||
if step <= Decimal::ZERO {
|
||||
return None;
|
||||
}
|
||||
let proxy = super::decimal_exact::mul_for_rounding(self, rhs, step.normalize().scale())?;
|
||||
super::round::round_step(proxy, step, mode)
|
||||
}
|
||||
fn checked_div_exact(self, rhs: Self) -> Option<Self> {
|
||||
let q = self.checked_div(rhs)?;
|
||||
(q.checked_mul(rhs) == Some(self)).then_some(q)
|
||||
|
||||
Reference in new issue
Block a user