refactor(core): the last eleven Decimal sites go through the kernel (K-U11a)
The detector now reads zero for ledger-core. These eleven waited for K-U10's exact operations, since each needed a decision the first 199 did not: - The frozen close rules (periods::try_allocate and try_allocate_last_absorbs) and Ledger::member_close_split multiply a share by a total. That product is a rounded figure by construction (a share of 27 places), and posted closes replay through the first two, so not a digit may move: they call Amount::checked_mul_rounded, the product they have always computed, by name. - The audit's check of a close (check_split) computes its bound and each member's drift through Amount. A bound or a drift that exact arithmetic cannot hold is raised as a finding, never passed as clean. - Four places changed how many decimal places a figure is written with (a contribution margin's one place, a schedule's months at the currency's places, Money's step count). Each called Decimal::rescale, which rounds half away from zero when asked for fewer places than the value has. None of them asked for that, and only a debug assertion said so. kernel::rescale_exact adds or drops trailing zeros and returns None for anything else, so a figure's written places can change and its value cannot. rescale_exact has no Kani harness and no Lean theorem: whole-number money has no places, so there is nothing for an i64 harness or a law over the rationals to state. It is the sixteenth listed exception of the traceability gate, and two bridge tests carry it instead: a property over Decimal's whole range against whole-number arithmetic that shares no code with it (None exactly when the amount is no whole multiple of the step; a result is the same number with the same sign), and a table of the figures the four call sites print. No figure moves. The full ledger-core suite passes. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
3918151794
commit
7d525d392e
9 files changed
+158
-27
No files matched your search
@@ -106,6 +106,36 @@ pub fn round_places(amount: Decimal, places: u32, mode: Rounding) -> Option<Deci
|
||||
Some(rounded)
|
||||
}
|
||||
|
||||
/// `amount` written with `places` decimal places, when that changes no
|
||||
/// digit of its value: trailing zeros are added or dropped, nothing else.
|
||||
/// This is how a figure is given the places it prints with (a margin of
|
||||
/// "100.0", a month of "0.25" that the kernel returned as 0.250), and it
|
||||
/// is the one such place: `Decimal::rescale` rounds half away from zero
|
||||
/// when asked for fewer places than the value has, silently, and this
|
||||
/// refuses instead.
|
||||
///
|
||||
/// ```text
|
||||
/// GOAL: change how many places an amount is written with, never the
|
||||
/// amount.
|
||||
/// SOURCE: BS-ledger-1 (law 40: a whole multiple of the step is its own
|
||||
/// rounding in every mode, so writing it at the step's places
|
||||
/// rounds nothing).
|
||||
/// INVARIANTS:
|
||||
/// 1. A result equals `amount`, and keeps its sign (a zero's too).
|
||||
/// 2. None exactly when `amount` is no whole multiple of 10^(−places)
|
||||
/// (it would have to be rounded), or `places` is past Decimal's 28.
|
||||
/// 3. A result carries `places` places, or, where Decimal's 96 bits
|
||||
/// cannot hold the amount that wide, as many as they can hold.
|
||||
/// ```
|
||||
pub fn rescale_exact(amount: Decimal, places: u32) -> Option<Decimal> {
|
||||
if places > Decimal::MAX_SCALE {
|
||||
return None;
|
||||
}
|
||||
let mut written = amount;
|
||||
written.rescale(places);
|
||||
(written == amount).then_some(written)
|
||||
}
|
||||
|
||||
/// `amount` rounded to whole `currency` minor units in `mode`: cents for
|
||||
/// USD, whole yen for JPY, thousandths for KWD. A currency ISO 4217 gives
|
||||
/// no minor unit (gold, SDRs) is returned exactly, as the FX booking rule
|
||||
|
||||
Reference in new issue
Block a user