refactor(core): every report and book total goes through the kernel's checked sum
A raw Decimal + / += / .sum() panics at Decimal::MAX, and a replayed journal line is accepted of any size (rules.md B-28), so a total of restored figures must be exact or refused (B-21), never a crash. The reports (P&L, balance sheet, general ledger, members' equity, asset register), K-1 lines, tax-basis wholes, payouts, the 1099 split, the audit's articulation check, the members close split and arap's document_total now sum with kernel::total / total_of / total_try and add or subtract with checked arithmetic, refusing with TooLargeToCompute. money::sum_exact* and add_exact/sub_exact are the engine's thin wrappers over the kernel (no second copy of the sum). form1099::sum3 was a copy of the kernel's sum and is now kernel::total. periods::allocate and allocate_last_absorbs (frozen replay rules) keep their signatures and results to the cent; each gains a try_ twin that returns TooLargeToCompute where the raw amount * share used to panic, and the ledger's close split calls the twins. Behaviour for every ordinary input is unchanged: no figure moves, no test or golden edited. Infallible public signatures (trial_balance, ProfitAndLossStatement::net_income, tax book_net, project_entries, cash-flow kinds, the Regime) keep their raw sums; making them fallible would change ~100 callers and is a decision, not a refactor. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b3d504067f
commit
3376c3d549
10 files changed
+368
-162
No files matched your search
@@ -704,9 +704,7 @@ fn entry_buckets(
|
||||
}
|
||||
|
||||
fn sum3(b: [Decimal; 3]) -> Result<Decimal, LedgerError> {
|
||||
b[0].checked_add(b[1])
|
||||
.and_then(|s| s.checked_add(b[2]))
|
||||
.ok_or_else(|| LedgerError::TooLargeToCompute("a payment's buckets".into()))
|
||||
kernel::total(b).ok_or_else(|| LedgerError::TooLargeToCompute("a payment's buckets".into()))
|
||||
}
|
||||
|
||||
/// The vendor's part of `payment` when the entry names several parties:
|
||||
@@ -763,13 +761,13 @@ fn split_share(b: [Decimal; 3], v: Decimal) -> Result<[Decimal; 3], LedgerError>
|
||||
let q = num.checked_div(ap).ok_or_else(too_large)?;
|
||||
let mut part = q.round_dp_with_strategy(2, rust_decimal::RoundingStrategy::ToZero);
|
||||
while times(part)? > num {
|
||||
part -= cent;
|
||||
part = part.checked_sub(cent).ok_or_else(too_large)?;
|
||||
}
|
||||
while times(part + cent)? <= num {
|
||||
part += cent;
|
||||
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 - times(part)?;
|
||||
rems[i] = num.checked_sub(times(part)?).ok_or_else(too_large)?;
|
||||
parts[i] = part;
|
||||
}
|
||||
kernel::apportion(av, &mut parts, &rems, cent).ok_or_else(too_large)?;
|
||||
|
||||
Reference in new issue
Block a user