diff --git a/crates/ledger-core/src/kernel/entries.rs b/crates/ledger-core/src/kernel/entries.rs index 5753b5d6..5964b48c 100644 --- a/crates/ledger-core/src/kernel/entries.rs +++ b/crates/ledger-core/src/kernel/entries.rs @@ -50,6 +50,28 @@ pub trait Amount: Copy + Ord { fn checked_mul_rounded(self, rhs: Self) -> Option { self.checked_mul(rhs) } + /// `self + rhs` as the amount type adds, which for [`Decimal`] is + /// `rust_decimal`'s: exact where the sum fits 96 bits and scale 28, + /// otherwise rounded to what it holds. `None` only on overflow. + /// + /// The engine's rounding sum, beside its rounding product and its + /// rounding division, and for one use: a running total of rounded + /// shares (`whole x weight / total`, each a quotient of 28 significant + /// digits). Three thirds of 28 digits are not a sum 96 bits hold, and + /// the total they stand for was never an exact figure: the caller + /// rounds it to the places it means before a cent is taken from it + /// (`tax_capital`'s `RunningSplit`, INVARIANT 4). Nothing else calls + /// it; a sum of amounts is [`Amount::checked_add`], exact or refused. + /// A machine integer never rounds, so there it is `checked_add`. + fn checked_add_rounded(self, rhs: Self) -> Option { + self.checked_add(rhs) + } + /// `self - rhs` as the amount type subtracts: [`Amount::checked_add_rounded`]'s + /// other half, for taking a rounded share back out of such a running + /// total. For a machine integer it is `checked_sub`. + fn checked_sub_rounded(self, rhs: Self) -> Option { + self.checked_sub(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. @@ -142,6 +164,12 @@ impl Amount for Decimal { fn checked_mul_rounded(self, rhs: Self) -> Option { Decimal::checked_mul(self, rhs) } + fn checked_add_rounded(self, rhs: Self) -> Option { + Decimal::checked_add(self, rhs) + } + fn checked_sub_rounded(self, rhs: Self) -> Option { + Decimal::checked_sub(self, rhs) + } fn checked_mul_round_to_step(self, rhs: Self, step: Self, mode: Rounding) -> Option { if let Some(product) = super::decimal_exact::exact_mul(self, rhs) { return super::round::round_step(product, step, mode); diff --git a/crates/ledger-core/src/tax_capital.rs b/crates/ledger-core/src/tax_capital.rs index 6255ad84..98bf0546 100644 --- a/crates/ledger-core/src/tax_capital.rs +++ b/crates/ledger-core/src/tax_capital.rs @@ -462,7 +462,11 @@ impl RunningSplit { } let mut exact = self.exact.clone(); for (x, w) in exact.iter_mut().zip(weights) { - *x = crate::kernel::Amount::checked_add(*x, Self::share(whole, *w, sum)?) + // A sum of rounded quotients, itself rounded where 96 bits + // end (INVARIANT 4 is what that leaves): three thirds of 28 + // digits are no exact Decimal, so the exact sum would refuse + // every split by thirds. + *x = crate::kernel::Amount::checked_add_rounded(*x, Self::share(whole, *w, sum)?) .ok_or_else(too_large)?; } let total = crate::kernel::Amount::checked_add(self.total, whole).ok_or_else(too_large)?; @@ -546,9 +550,12 @@ impl RunningSplit { let mut exact = self.exact.clone(); let mut given = self.given.clone(); for i in 0..n { - exact[i] = - crate::kernel::Amount::checked_sub(exact[i], Self::share(whole, weights[i], sum)?) - .ok_or_else(too_large)?; + // The rounding difference `split`'s sum is the other half of. + exact[i] = crate::kernel::Amount::checked_sub_rounded( + exact[i], + Self::share(whole, weights[i], sum)?, + ) + .ok_or_else(too_large)?; given[i] = crate::kernel::Amount::checked_sub(given[i], parts[i]).ok_or_else(too_large)?; } diff --git a/crates/ledger-core/tests/kernel_bridge/decimal_exact.rs b/crates/ledger-core/tests/kernel_bridge/decimal_exact.rs index bf93ee2d..6f9f576e 100644 --- a/crates/ledger-core/tests/kernel_bridge/decimal_exact.rs +++ b/crates/ledger-core/tests/kernel_bridge/decimal_exact.rs @@ -125,6 +125,42 @@ proptest! { prop_assert_eq!(got.is_some(), representable(&exact), "{} - {}", a, b); } + /// The rounding sum and difference (a running total of rounded shares, + /// K-U11): `rust_decimal`'s own, to the digit and the scale; the exact + /// figure wherever exact arithmetic gives one; and where it rounds, + /// within half a unit of the last place it keeps. + #[test] + fn the_rounding_sum_is_the_exact_one_wherever_that_exists((a, b) in arb_pair()) { + for (got, old, exact_op, exact) in [ + ( + Amount::checked_add_rounded(a, b), + Decimal::checked_add(a, b), + Amount::checked_add(a, b), + rat(a) + rat(b), + ), + ( + Amount::checked_sub_rounded(a, b), + Decimal::checked_sub(a, b), + Amount::checked_sub(a, b), + rat(a) - rat(b), + ), + ] { + prop_assert_eq!(shown(got), shown(old), "{} and {}", a, b); + if exact_op.is_some() { + prop_assert_eq!(shown(got), shown(exact_op), "{} and {}", a, b); + } + if let Some(r) = got { + let off = rat(r) - exact; + let half_unit = BigRational::new( + BigInt::from(1u8), + BigInt::from(2u8) * BigInt::from(10u8).pow(r.scale()), + ); + let off = if off < BigRational::from_integer(BigInt::from(0u8)) { -off } else { off }; + prop_assert!(off <= half_unit, "{} and {} gave {}", a, b, r); + } + } + } + /// Multiply: the same laws. #[test] fn mul_is_exact_or_none((a, b) in arb_pair()) { diff --git a/crates/ledger-core/tests/tax_capital.proptest-regressions b/crates/ledger-core/tests/tax_capital.proptest-regressions index 45fdd12b..08f7ae82 100644 --- a/crates/ledger-core/tests/tax_capital.proptest-regressions +++ b/crates/ledger-core/tests/tax_capital.proptest-regressions @@ -5,3 +5,4 @@ # It is recommended to check this file in to source control so that # everyone who runs the test benefits from these saved cases. cc 56101dd675399c931df47369972f415782f4cd0e6ea874c37ef655b9961b69cb # shrinks to n = 2, splits = [(28343, [4, 2, 0, 0]), (27468, [0, 1, 0, 0]), (82432, [2, 4, 0, 0]), (-19139, [1, 2, 0, 0])] +cc 9d3652b30f1cf76da6d8dad78a402ff774ddf30ea760574a8c1ebed15e684684 # shrinks to n = 2, splits = [(1, [4, 2, 0, 0], 1), (990, [0, 1, 0, 0], 1)]