refactor(core): one rounding primitive, round_step, in the kernel
Every money round_dp*/rescale in ledger-core now goes through kernel::round_step: amount to a whole multiple of a step, in a named mode (half away, half even, toward zero, toward negative infinity), exact integer arithmetic on Decimal mantissas and scales, None instead of a panic, no negative zero. The call sites use round_places, which reproduces Decimal::round_dp_with_strategy exactly (value, scale and its early return), and add_line_fx uses round_minor for the base currency's smallest unit. This is a refactor: every site produces exactly what it produced before, so no golden changes. The schedules' monthly-even plan keeps its two-place cent rule: moving it to the currency's minor unit is K-U5's decision (D-72), not this behaviour-preserving unit. The remaining rescales (money.rs's display padding, the contribution margin's one place) are formatting, not rounding, and stay. Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
1 parent
75c666a7ce
commit
74943fb699
12 files changed
+375
-41
No files matched your search
@@ -728,7 +728,11 @@ fn vendor_part(payment: Decimal, share: Decimal, target: Decimal) -> Result<Deci
|
||||
"a vendor's share ({share}) of a payment of {payment}"
|
||||
))
|
||||
})?;
|
||||
Ok(scaled.round_dp_with_strategy(2, rust_decimal::RoundingStrategy::MidpointAwayFromZero))
|
||||
kernel::round_places(scaled, 2, kernel::Rounding::HalfAwayFromZero).ok_or_else(|| {
|
||||
LedgerError::TooLargeToCompute(format!(
|
||||
"a vendor's share ({share}) of a payment of {payment}"
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -761,7 +765,8 @@ fn split_share(b: [Decimal; 3], v: Decimal) -> Result<[Decimal; 3], LedgerError>
|
||||
// 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 = q.round_dp_with_strategy(2, rust_decimal::RoundingStrategy::ToZero);
|
||||
let mut part =
|
||||
kernel::round_places(q, 2, kernel::Rounding::TowardZero).ok_or_else(too_large)?;
|
||||
while times(part)? > num {
|
||||
part -= cent;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user