refactor(core): proportional shares in the kernel, one copy

The K-1/1065 worksheet, the 1099 worksheet, the parties' shares, the
conventions' template split and the cash-flow government split each
computed "whole x weight / total, round, hand out the leftover" for
themselves. Six copies meant six places for a rounding or sign change to
reach a filed figure.

Put the shape in kernel::allocate: share_of (the engine's division,
rounded at Decimal's 28 significant digits, truncating on a machine
integer), share_floor_with (the exact floor to a step by multiplication,
the 1099 correction loop's arithmetic), and split_by_weights_with (the
split, with the leftover policy named: largest remainder, first absorbs,
last absorbs; the running policy stays apportion_cumulative). Each site
keeps its own mode, step, sign and leftover policy, so every figure is
the same to the digit; the frozen periods rules only gain a doc line
naming the kernel policy they equal.

Amount gains checked_div, the engine's division as distinct from
checked_div_exact.

All goldens and existing tests pass unchanged.

Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
swaitsandDeepSeek V4.1 Flash committed 2026-11-09 00:14:26 -07:00
1 parent 127d33fb0b
commit 24b59e7b85
9 files changed
+394 -84

No files matched your search

+14 -23
View File
@@ -718,14 +718,11 @@ fn vendor_part(payment: Decimal, share: Decimal, target: Decimal) -> Result<Deci
} else if target.is_zero() {
Ok(Decimal::ZERO)
} else {
let scaled = payment
.checked_mul(share)
.and_then(|x| x.checked_div(target))
.ok_or_else(|| {
LedgerError::TooLargeToCompute(format!(
"a vendor's share ({share}) of a payment of {payment}"
))
})?;
let scaled = crate::kernel::share_of(payment, share, target).ok_or_else(|| {
LedgerError::TooLargeToCompute(format!(
"a vendor's share ({share}) of a payment of {payment}"
))
})?;
kernel::round_places(scaled, 2, kernel::Rounding::HalfAwayFromZero).ok_or_else(|| {
LedgerError::TooLargeToCompute(format!(
"a vendor's share ({share}) of a payment of {payment}"
@@ -752,28 +749,22 @@ fn split_share(b: [Decimal; 3], v: Decimal) -> Result<[Decimal; 3], LedgerError>
}
let (av, ap) = (v.abs(), p.abs());
let cent = Decimal::new(1, 2);
let times = |x: Decimal| x.checked_mul(ap).ok_or_else(too_large);
let mut parts = [Decimal::ZERO; 3];
let mut rems = [Decimal::ZERO; 3];
for i in 0..3 {
if b[i].is_zero() {
continue;
}
// The exact share is num / ap: rounded down to the cent, checked
// exactly (the division alone rounds past 28 digits).
let num = b[i].abs().checked_mul(av).ok_or_else(too_large)?;
let q = num.checked_div(ap).ok_or_else(too_large)?;
let mut part =
kernel::round_places(q, 2, kernel::Rounding::TowardZero).ok_or_else(too_large)?;
while times(part)? > num {
part = part.checked_sub(cent).ok_or_else(too_large)?;
}
while times(part.checked_add(cent).ok_or_else(too_large)?)? <= num {
part = part.checked_add(cent).ok_or_else(too_large)?;
}
// The remainder, over the common denominator ap.
rems[i] = num.checked_sub(times(part)?).ok_or_else(too_large)?;
// The exact share is |b[i]| × av ÷ ap, floored to the cent by
// multiplication (the division alone rounds past 28 digits); the
// kernel returns the exact floor and the exact remainder over the
// common denominator ap.
let (part, rem) = kernel::share_floor_with(b[i].abs(), av, ap, cent, |x| {
kernel::round_places(x, 2, kernel::Rounding::TowardZero)
})
.ok_or_else(too_large)?;
parts[i] = part;
rems[i] = rem;
}
kernel::apportion(av, &mut parts, &rems, cent).ok_or_else(too_large)?;
if v.is_sign_negative() {