fix(core): a running total of rounded shares adds by the rounding sum (K-U11a)
After K-U10 made the kernel's Decimal sum exact or refused, the K-1 worksheet refused every split by thirds with "a member's tax-basis capital is too large to compute". Two tests caught it (thirds_summed_are_the_cents_they_are, a_running_split_never_drifts_a_cent); it never reached main, where the worksheet still called rust_decimal's own sum. A member's running total in RunningSplit is a sum of shares, each whole x weight / sum of weights: a quotient of 28 significant digits. Two thirds of 28 digits do not add to a number 96 bits hold. The total they stand for was never an exact figure, and INVARIANT 4 of that type was written for exactly that: the total is rounded to ten places before a cent is taken from it. So the fault was in the routing, not the arithmetic: K-U11a's script mapped that sum to the exact operation. A sum of rounded quotients is a rounded figure by construction, as a share's product is (checked_mul_rounded, K-U10). The kernel now names that sum too: Amount::checked_add_rounded and checked_sub_rounded, rust_decimal's own for Decimal and the plain operation for a machine integer. RunningSplit::split and unsplit call them and nothing else does; every sum of amounts stays exact or refused. So the K-1 worksheet computes what it computes on main, to the digit. The bridge property checks, over Decimal's whole range: the rounding sum is rust_decimal's figure to the digit and the scale, it is the exact figure wherever exact arithmetic gives one, and where it rounds it is within half a unit of the last place it keeps. The seed proptest shrank the failure to is kept. Every other place outside the kernel that makes a figure of more than ten places was read for the same fault (docs/proofs/k-u11.md lists them); none adds such a figure before rounding it. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
7d525d392e
commit
34f5275ec4
4 files changed
+76
-4
No files matched your search
@@ -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()) {
|
||||
|
||||
Reference in new issue
Block a user