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:
1 parent
127d33fb0b
commit
24b59e7b85
9 files changed
+394
-84
No files matched your search
@@ -454,27 +454,30 @@ pub fn check_id(id: &str) -> Result<(), String> {
|
||||
/// ([`crate::kernel::apportion`]): the parts sum to `amount` exactly
|
||||
/// (INVARIANT 2).
|
||||
pub fn split(amount: Decimal, shares: &[Decimal], places: Option<u32>) -> Option<Vec<Decimal>> {
|
||||
use crate::kernel::{Rounding, round_places};
|
||||
let exact: Vec<Decimal> = shares
|
||||
.iter()
|
||||
.map(|s| amount.checked_mul(*s))
|
||||
.collect::<Option<_>>()?;
|
||||
use crate::kernel::{Leftover, Rounding, round_places, split_by_weights_with};
|
||||
let Some(places) = places else {
|
||||
let mut parts = exact;
|
||||
crate::kernel::plug_last(amount, &mut parts)?;
|
||||
return Some(parts);
|
||||
// No rounding: each part is amount × share, the last absorbing
|
||||
// any difference (the exact split's own plug).
|
||||
return split_by_weights_with(
|
||||
amount,
|
||||
shares,
|
||||
Decimal::ONE,
|
||||
Decimal::ONE,
|
||||
Some,
|
||||
Leftover::LastAbsorbs,
|
||||
);
|
||||
};
|
||||
let mut parts: Vec<Decimal> = exact
|
||||
.iter()
|
||||
.map(|e| round_places(*e, places, Rounding::TowardZero))
|
||||
.collect::<Option<_>>()?;
|
||||
let remainders: Vec<Decimal> = exact
|
||||
.iter()
|
||||
.zip(&parts)
|
||||
.map(|(e, p)| e.checked_sub(*p))
|
||||
.collect::<Option<_>>()?;
|
||||
crate::kernel::apportion(amount, &mut parts, &remainders, Decimal::new(1, places))?;
|
||||
Some(parts)
|
||||
// amount × share, each part truncated toward zero to `places`, the
|
||||
// leftover by largest remainder. The shares sum to one by the
|
||||
// conventions' own validation, so the total is one, never their sum.
|
||||
split_by_weights_with(
|
||||
amount,
|
||||
shares,
|
||||
Decimal::ONE,
|
||||
Decimal::new(1, places),
|
||||
|x| round_places(x, places, Rounding::TowardZero),
|
||||
Leftover::LargestRemainder,
|
||||
)
|
||||
}
|
||||
|
||||
impl Conventions {
|
||||
|
||||
Reference in new issue
Block a user