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
@@ -258,6 +258,73 @@ proptest! {
|
||||
prop_assert_eq!(got.to_string(), want.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
/// `rescale_exact` changes how an amount is written, never the
|
||||
/// amount. Over Decimal's whole range, against whole-number
|
||||
/// arithmetic that shares nothing with it: it refuses exactly when
|
||||
/// the amount is no whole multiple of 10^-places, and what it returns
|
||||
/// is the same value with the same sign, at `places` places or, where
|
||||
/// 96 bits cannot hold it that wide, as many as they can.
|
||||
#[test]
|
||||
fn rescale_exact_never_changes_the_value(
|
||||
mantissa in prop_oneof![
|
||||
-(1i128 << 60)..=(1i128 << 60),
|
||||
-MAX_MANTISSA..=MAX_MANTISSA,
|
||||
(0u32..=27, -9i128..=9).prop_map(|(zeros, digit)| digit * 10i128.pow(zeros)),
|
||||
],
|
||||
scale in 0u32..=28,
|
||||
places in 0u32..=28,
|
||||
) {
|
||||
use num_bigint::BigInt;
|
||||
let amount = Decimal::try_from_i128_with_scale(mantissa, scale).expect("in Decimal's range");
|
||||
let whole_multiple = places >= scale || mantissa % 10i128.pow(scale - places) == 0;
|
||||
let got = ledger_core::kernel::rescale_exact(amount, places);
|
||||
prop_assert_eq!(got.is_some(), whole_multiple, "{} at {} places", amount, places);
|
||||
if let Some(written) = got {
|
||||
// The same number: both mantissas brought to 28 places.
|
||||
let at_28 = |m: i128, s: u32| BigInt::from(m) * BigInt::from(10u8).pow(28 - s);
|
||||
prop_assert_eq!(at_28(written.mantissa(), written.scale()), at_28(mantissa, scale));
|
||||
prop_assert_eq!(written.is_sign_negative(), amount.is_sign_negative());
|
||||
let no_room = written.mantissa().unsigned_abs() * 10 > MAX_MANTISSA.unsigned_abs();
|
||||
prop_assert!(
|
||||
written.scale() == places || (written.scale() < places && no_room),
|
||||
"{} written as {} at {} places", amount, written, places
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Decimal's largest mantissa: 96 bits.
|
||||
const MAX_MANTISSA: i128 = (1i128 << 96) - 1;
|
||||
|
||||
/// `rescale_exact` at the edges, each row checked by hand: the places a
|
||||
/// figure prints with, a zero's sign, and the two refusals.
|
||||
#[test]
|
||||
fn rescale_exact_at_the_edges() {
|
||||
use ledger_core::kernel::rescale_exact;
|
||||
let text =
|
||||
|amount: &str, places: u32| rescale_exact(parse(amount), places).map(|d| d.to_string());
|
||||
assert_eq!(text("100", 1).as_deref(), Some("100.0"));
|
||||
assert_eq!(text("0.250", 2).as_deref(), Some("0.25"));
|
||||
assert_eq!(text("0", 2).as_deref(), Some("0.00"));
|
||||
assert_eq!(text("12.5", 1).as_deref(), Some("12.5"));
|
||||
assert_eq!(text("-3.10", 1).as_deref(), Some("-3.1"));
|
||||
// A digit would be lost: refused, where `Decimal::rescale` rounds.
|
||||
assert_eq!(text("0.255", 2), None);
|
||||
assert_eq!(text("-0.001", 2), None);
|
||||
assert_eq!(text("1", 29), None);
|
||||
// A negative zero keeps its sign and takes the places (a schedule's
|
||||
// posted month is compared as text on replay).
|
||||
let minus_zero = rescale_exact(-Decimal::ZERO, 2).unwrap();
|
||||
assert!(minus_zero.is_zero() && minus_zero.is_sign_negative());
|
||||
assert_eq!(minus_zero.scale(), 2);
|
||||
// No room for two more places in 96 bits: as many as fit, the value
|
||||
// untouched.
|
||||
let wide = Decimal::from_i128_with_scale(MAX_MANTISSA, 0);
|
||||
assert_eq!(rescale_exact(wide, 2), Some(wide));
|
||||
assert_eq!(rescale_exact(wide, 2).unwrap().scale(), 0);
|
||||
let nearly = Decimal::from_i128_with_scale(MAX_MANTISSA / 10, 0);
|
||||
assert_eq!(rescale_exact(nearly, 2).unwrap().scale(), 1);
|
||||
}
|
||||
|
||||
/// The edge table, each row checked by hand.
|
||||
|
||||
Reference in new issue
Block a user