test(core): exact Decimal arithmetic refuses nothing it could hold

The independent review of K-U10 found the bridge properties proved one
direction only. They compared the kernel with an expectation built from
rust_decimal's own result, so they showed a figure returned is exact, and
could not show that an exact figure is never refused. Each of add,
subtract, multiply and exact division is now also Some exactly when the
rational result is a Decimal (a mantissa under 2^96 at a scale of 0 to
28), decided from the rational alone. All hold: no change to the kernel.

Also from the review:
- the FX product rounded once is checked at steps of every scale, not 0 to
  3 places, and a None is accepted only at the two bounds the code states;
- one refusal the docs did not state is now stated and pinned: a product
  that is not exact, at a step of 27 or 28 places, is None though its
  rounded figure can be a Decimal (the one rounding needs two places
  below the step and Decimal ends at 28). No currency's step is near it;
- running totals are checked over thousands of lines, and the first
  refusal is pinned (line 7,923 of the largest ten-place amount, where
  rust_decimal drops a place and rounds);
- three stale sentences: percent_of's product is exact or refused since
  this unit (only its division rounds), the header of allocate.rs, and
  the table in docs/proofs/k-u10.md, which now records the review.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-09 22:18:54 -07:00
1 parent e46fa4fc6e
commit 5777787b69
6 files changed
+210 -23

No files matched your search

+43 -4
View File
@@ -65,7 +65,9 @@ full-scale mantissa passes 96 bits but whose last digits are zeros, like
`100 x 0.3333333333333333333333333333` = 33.33…3300, which `percent_of`
computes for every ratio, or `10^20 x 10^-20` squared) and `periods.rs`'s
hand-written scale check does exactly that. Comparing values is the
complete test, so nothing exact is refused (`an_exact_product_that_only_looks_wide_is_kept`).
complete test, so nothing exact is refused (`an_exact_product_that_only_looks_wide_is_kept`,
and law 5 of the bridge properties: `Some` exactly when the exact result
is a `Decimal`, decided from the rational alone).
`Amount::checked_sub` for `Decimal` is `rust_decimal`'s subtraction under
the same check. The trait default (`self + (-rhs)`) gave `-0` for `0 - 0`;
@@ -122,15 +124,18 @@ independent `num_rational::BigRational` reference, over every mantissa to
| Property | Law |
|---|---|
| `add_is_exact_or_none` | `Some(r)` equals the rational sum; equals the old result to the character where that was exact; `None` where it was rounded or `None` |
| `add_is_exact_or_none` | `Some(r)` equals the rational sum; equals the old result to the character where that was exact; `None` where it was rounded or `None`; and `Some` exactly when the rational sum is a `Decimal` (a mantissa under 2^96 at a scale of 0 to 28), decided with none of `rust_decimal`'s arithmetic, so nothing representable is refused |
| `sub_is_exact_or_none` | the same, against `rust_decimal`'s subtraction (zero included) |
| `mul_is_exact_or_none` | the same, for products |
| `a_refusal_is_an_overflow_or_a_rounding` | the kernel refuses only where `rust_decimal` overflowed or rounded |
| `posted_range_sums_are_unchanged` | every line-range figure (≤ 10^15, ≤ 10 places, any raw scale holding it): add and subtract equal the old to the character and are never `None` |
| `posted_range_running_totals_are_unchanged` | a running total of up to 100 line-range figures is unchanged at every step |
| `posted_range_running_totals_of_thousands_of_lines_are_unchanged` | the same for 4,000 to 5,000 lines, all of one sign in half the cases, where the total's mantissa comes within a factor of two of 2^96 |
| `a_running_total_is_refused_where_it_would_first_round` | 7,922 lines of 999999999999999.9999999999 sum exactly; the 7,923rd passes 2^96 at ten places, where `rust_decimal` drops a place and rounds and the kernel refuses |
| `posted_range_fx_products_are_unchanged_or_refused` | foreign (≤ 10^25 mantissa, ≤ 10 places) x rate (≤ 10 places): identical to the old whenever the mantissa product fits 96 bits; otherwise the old figure only if it was exact, else `None` |
| `the_fx_product_is_rounded_once_from_the_exact_one` | `checked_mul_round_to_step` equals the exact rational product rounded once, in each of four modes, at steps of 1, 5 and 25 units of 1, 0.1, 0.01 and 0.001 (a step that is not a power of ten has its half steps one place below it), signs and ties included; `None` only past about 10^23 |
| `div_exact_is_exact_or_none` | a quotient `checked_div_exact` returns, times the divisor, is the dividend as a rational (`a_rounded_quotient_is_not_an_exact_one` names the thirds, sevenths and ninths) |
| `the_fx_product_is_rounded_once_from_the_exact_one` | `checked_mul_round_to_step` equals the exact rational product rounded once, in each of four modes, at steps of 1, 5 and 25 units of 1, 0.1, 0.01 and 0.001 (a step that is not a power of ten has its half steps one place below it), signs and ties included, and at steps of any number of places up to 28; `None` only where the exact product times 10^(the step's places + 2) does not fit 96 bits, or the step has 27 or 28 places (`a_step_of_27_places_refuses_a_product_that_is_not_exact` pins that one) |
| `div_exact_is_exact_or_none` | a quotient `checked_div_exact` returns, times the divisor, is the dividend as a rational (`a_rounded_quotient_is_not_an_exact_one` names the thirds, sevenths and ninths), and it returns one whenever the exact quotient is a `Decimal` |
| `div_exact_over_the_whole_range` | the same over every `Decimal`, half the dividends built as a product so the exact quotient exists |
Also: the three flipped canaries (below), `an_exact_product_that_only_looks_wide_is_kept`,
`zero_is_exact`, `the_k_u1_edge_books_the_cent_the_exact_products_cent`
@@ -171,3 +176,37 @@ each a rounded figure that is now refused or correctly rounded):
because `periods.rs` shares by a many-place percent, for one, relies on a
rounded product, and each is unreachable below about 7.9e26. A follow-up
can convert them one by one against its own refusal.
## What the independent review found
A second model read the unit before it merged (2026-11-09) and sampled
400,000 random pairs against the 256-bit arithmetic: no wrong `Some`. Its
findings, each reproduced before it was acted on:
- **One refusal the docs did not state.** `checked_mul_round_to_step`
returns `None` for a product that is not exact at a step of 27 or 28
places, though the rounded figure can be a `Decimal` (1e-28 squared to
a step of 1e-27 is zero). The one rounding needs a real digit and a
sticky digit below the step, and `Decimal` has no place for them past
28. Kept, stated in the trait and in `mul_for_rounding`, and pinned by a
test: it is a refusal, no step in any books is near it, and removing it
means dividing in 256 bits for a case nothing reaches.
- **The properties proved soundness, not completeness.** They compared
the kernel with an expectation built from `rust_decimal`'s own result,
so a refusal of something exact could not fail them. Now each of add,
subtract, multiply and exact division is also `Some` exactly when the
rational result is a `Decimal`, decided from the rational alone. All
hold; nothing was refused that should not be.
- **The FX property reached steps of 0 to 3 places only** and accepted any
`None` past 10^23. It now covers steps of every scale and accepts a
`None` only at the two bounds the code documents.
- **Running totals reached 100 lines.** Now thousands, to within a factor
of two of the mantissa's end, and the first refusal is pinned (line
7,923 of the largest ten-place amount).
- **Three stale sentences**: `percent_of`'s product (exact or refused
since this unit, only the division rounds), the header of
`allocate.rs`, and this page's table.
The sampling is a test, not a proof: the 256-bit helpers have no Kani
harness of their own (see "Kani" above), and rest on the bridge
properties and the unit tests against `num_bigint`.