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
@@ -343,7 +343,7 @@ pub(crate) fn share_property(
|
||||
credits: &[Decimal],
|
||||
places: u32,
|
||||
) -> Result<Vec<Decimal>, LedgerError> {
|
||||
use crate::kernel::{Rounding, round_places};
|
||||
use crate::kernel::{Leftover, Rounding, round_places, split_by_weights_with};
|
||||
let too_large =
|
||||
|| LedgerError::TooLargeToCompute("a member's share of contributed property".into());
|
||||
let total = credits
|
||||
@@ -354,22 +354,18 @@ pub(crate) fn share_property(
|
||||
if whole <= Decimal::ZERO || total <= Decimal::ZERO {
|
||||
return Ok(vec![Decimal::ZERO; credits.len()]);
|
||||
}
|
||||
// whole × credit is exact; ÷ total is exact whenever the share is a
|
||||
// whole number of units, so truncating never loses a unit it is owed.
|
||||
let exact: Vec<Decimal> = credits
|
||||
.iter()
|
||||
.map(|c| whole.checked_mul(*c).and_then(|x| x.checked_div(total)))
|
||||
.collect::<Option<_>>()
|
||||
.ok_or_else(too_large)?;
|
||||
let mut parts: Vec<Decimal> = exact
|
||||
.iter()
|
||||
.map(|x| round_places(*x, places, Rounding::TowardZero))
|
||||
.collect::<Option<_>>()
|
||||
.ok_or_else(too_large)?;
|
||||
let remainders: Vec<Decimal> = exact.iter().zip(&parts).map(|(x, p)| x - p).collect();
|
||||
crate::kernel::apportion(whole, &mut parts, &remainders, Decimal::new(1, places))
|
||||
.expect("each part is short by under a unit, so the shortfall is under a unit per member");
|
||||
Ok(parts)
|
||||
// whole × credit ÷ total, each part truncated toward zero to the
|
||||
// currency's places, the leftover units by largest remainder. The
|
||||
// kernel is the one copy of the shape (BS-partner-4's entry split).
|
||||
split_by_weights_with(
|
||||
whole,
|
||||
credits,
|
||||
total,
|
||||
Decimal::new(1, places),
|
||||
|x| round_places(x, places, Rounding::TowardZero),
|
||||
Leftover::LargestRemainder,
|
||||
)
|
||||
.ok_or_else(too_large)
|
||||
}
|
||||
|
||||
/// The party a member (by name, as in wanderbooks.toml) is in the
|
||||
|
||||
Reference in new issue
Block a user