fix(core): Decimal arithmetic in ledger-core goes through the kernel (K-U11a)
Bar item 1: money arithmetic lives in kernel/ and nowhere else. scripts/raw-decimal-arith.sh listed 210 places in ledger-core that did not: raw +, -, * and unary minus (which panic past Decimal's range), and checked_add / checked_sub / checked_mul / checked_div calls that resolved to rust_decimal's inherent methods (which round silently past 28 digits) and not to the kernel's Amount. 199 of them now call the kernel: Amount's methods by their full path, total / total_of for the hand-rolled sums, normal_balance, and money.rs's sign and in-range helpers. The rewrite was done by a script from the compiler's own line and column for each site, so no site was copied by hand; the few that needed a decision are explicit: - a balance sheet that cannot add liabilities and equity is not balanced (it panicked); - a template's shares, a party's percents, a reconciliation's book and cleared balances and a reversal's document movements refuse with TooLargeToCompute or their own message where they panicked; - tax_capital's split refuses where a product by the sign could not be taken, and collects its remainders as Options. No figure moves: every site computes what it computed, by the same operations in the same order. Left, each for a named reason (the detector prints these 11): money.rs 139 and 150 and periods.rs 149 and 202 (K-U10a rewrites them), audit.rs 355 and 511 (V-D3 is in them), ledger.rs 4048 (a share times the closed total: checked_mul_rounded, with K-U10a), and four rescale calls (money.rs 302, reports.rs 637, schedules.rs 370 and 377) that need a kernel function of their own. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
e0c78a783b
commit
32725fb669
19 files changed
+401
-394
No files matched your search
@@ -14,6 +14,7 @@
|
||||
//! through the *next* open period (or a reversal dated in an open period).
|
||||
|
||||
use crate::LedgerError;
|
||||
use crate::money::neg;
|
||||
use chrono::NaiveDate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -157,13 +158,13 @@ pub fn try_allocate(
|
||||
let remainders: Vec<Decimal> = exact
|
||||
.iter()
|
||||
.zip(&parts)
|
||||
.map(|(e, p)| e.checked_sub(*p).ok_or_else(too_large))
|
||||
.map(|(e, p)| crate::kernel::Amount::checked_sub(*e, *p).ok_or_else(too_large))
|
||||
.collect::<Result<_, _>>()?;
|
||||
crate::kernel::apportion(magnitude, &mut parts, &remainders, Decimal::new(1, 2))
|
||||
.ok_or_else(too_large)?;
|
||||
if amount.is_sign_negative() {
|
||||
for p in &mut parts {
|
||||
*p = -*p;
|
||||
*p = neg(*p);
|
||||
}
|
||||
}
|
||||
Ok(parts)
|
||||
@@ -270,7 +271,10 @@ pub fn allocate_cumulative(
|
||||
}
|
||||
let loss = total < Decimal::ZERO;
|
||||
let magnitude = total.abs().normalize();
|
||||
let given: Vec<Decimal> = given.iter().map(|&g| if loss { -g } else { g }).collect();
|
||||
let given: Vec<Decimal> = given
|
||||
.iter()
|
||||
.map(|&g| if loss { neg(g) } else { g })
|
||||
.collect();
|
||||
let mut parts = Vec::with_capacity(shares.len());
|
||||
let mut remainders = Vec::with_capacity(shares.len());
|
||||
for share in shares {
|
||||
@@ -278,8 +282,7 @@ pub fn allocate_cumulative(
|
||||
// Exact exactly when Decimal kept every digit: a product it had to
|
||||
// round comes back with fewer places than its factors carry (a
|
||||
// zero product, which it returns without places, is exact).
|
||||
let exact = magnitude
|
||||
.checked_mul(share)
|
||||
let exact = crate::kernel::Amount::checked_mul(magnitude, share)
|
||||
.filter(|e| e.is_zero() || e.scale() == magnitude.scale() + share.scale())
|
||||
.ok_or_else(|| {
|
||||
LedgerError::TooLargeToCompute(format!(
|
||||
@@ -290,7 +293,11 @@ pub fn allocate_cumulative(
|
||||
LedgerError::TooLargeToCompute(format!("a member's share {share} of {magnitude}"))
|
||||
})?;
|
||||
parts.push(floor);
|
||||
remainders.push(exact - floor);
|
||||
remainders.push(crate::money::sub_exact(
|
||||
exact,
|
||||
floor,
|
||||
"a member's share of the period's income",
|
||||
)?);
|
||||
}
|
||||
crate::kernel::apportion_cumulative(
|
||||
magnitude,
|
||||
@@ -302,7 +309,7 @@ pub fn allocate_cumulative(
|
||||
.ok_or_else(|| LedgerError::TooLargeToCompute(format!("the members' shares of {total}")))?;
|
||||
if loss {
|
||||
for p in parts.iter_mut().filter(|p| !p.is_zero()) {
|
||||
*p = -*p;
|
||||
*p = neg(*p);
|
||||
}
|
||||
}
|
||||
Ok(parts)
|
||||
|
||||
Reference in new issue
Block a user