test(core): the Decimal edges are refused, not rounded
The bridge compares impl Amount for Decimal's add, subtract and multiply with rust_decimal's own result and with an independent BigRational reference over every mantissa to 2^96 - 1 and every scale 0 to 28: a Some is the exact result; an exact old result is returned unchanged, compared as strings; a rounded one is None; an overflow stays None. Over the posted range (line cap, ten places, any raw scale) sums, differences, running totals and FX products are identical to the old to the character, and the FX product is checked rounded once from the exact one in all four rounding modes. Unit tests check the private 256-bit helpers against num_bigint. The pinned findings now assert the law holding: finding_decimal_add_rounds_dust_at_the_edge and finding_decimal_add_rounds_scale_near_the_top become a_sum_past_exact_arithmetic_is_refused, the_mantissa_edge_is_outside_the_precondition_and_pinned becomes the_mantissa_edge_books_the_exact_products_cent (10500000099.89, which the reference gives; the old 99.90 was a double rounding). Expectations that moved are all outside what a posted line carries: 599 parts of 1.3e26 at the cent sum past 96 bits (schedule::the_edges), the 26-place product now rounds once to 0.00, and the replaced-chain comparison refuses a step rust_decimal rounded. No golden changed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
38da08b2c2
commit
786e503858
7 files changed
+559
-80
No files matched your search
@@ -121,23 +121,22 @@ fn decimal_edge_table() {
|
||||
);
|
||||
}
|
||||
|
||||
/// FINDING (K-U4), pinned as a canary: `rust_decimal` rounds sub-unit dust
|
||||
/// away when a sum lands on `Decimal::MAX`, so `Amount::checked_add` for
|
||||
/// `Decimal` is not exact at the very edge (`MAX + 0.4 = MAX`, but `MAX +
|
||||
/// 0.5` is `None`). The kernel's "exact or `None`" contract (and `total`'s
|
||||
/// invariant 2) therefore holds for `Decimal` everywhere except figures
|
||||
/// within one unit of `Decimal::MAX` (~7.9 x 10^28, 10^13 times a journal
|
||||
/// line's cap). The fix belongs in `impl Amount for Decimal`
|
||||
/// (`kernel/entries.rs`), not in `total`; when it lands this test fails
|
||||
/// and is flipped to expect `None`. See docs/proofs/k-u4.md.
|
||||
/// K-U4's finding, closed by K-U10: `rust_decimal` rounds sub-unit dust
|
||||
/// away when a sum lands on `Decimal::MAX` (`MAX + 0.4 = MAX`, but `MAX +
|
||||
/// 0.5` is `None`), so `Amount::checked_add` for `Decimal` was not "exact
|
||||
/// or `None`" within one unit of `Decimal::MAX`. `impl Amount for Decimal`
|
||||
/// now checks the sum is the exact one, so `total`'s invariant 2 holds
|
||||
/// there too: the rounded sum is refused. See docs/proofs/k-u4.md and
|
||||
/// k-u10.md.
|
||||
#[test]
|
||||
fn finding_decimal_add_rounds_dust_at_the_edge() {
|
||||
fn a_sum_past_exact_arithmetic_is_refused() {
|
||||
let max = Decimal::MAX;
|
||||
let dust = Decimal::new(4, 1);
|
||||
assert_eq!(
|
||||
kernel::total([max, dust]),
|
||||
rust_decimal::Decimal::checked_add(max, dust),
|
||||
Some(max),
|
||||
"rust_decimal rounds the 0.4 away"
|
||||
"rust_decimal rounds the 0.4 away; the kernel does not"
|
||||
);
|
||||
assert_eq!(kernel::total([max, dust]), None);
|
||||
assert_eq!(kernel::total([max, Decimal::new(5, 1)]), None);
|
||||
}
|
||||
Reference in new issue
Block a user