Files
wanderbooks/docs/proofs/k-u10.md
T
swaitsandClaude 5777787b69 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>
2026-11-09 22:18:54 -07:00

13 KiB

K-U10: Decimal's sums and products are exact or refused (kernel/decimal_exact.rs)

Folded into docs/verification.md by the integrator; this note is the unit's own record.

Plan unit K-U10a (plan/p14-bookspec.md, the K-U10 row). Rules: BS-ledger-1 (a product the arithmetic cannot hold MUST be refused; a foreign amount is booked as the exact product rounded once) and BS-entry-4 (what a line can carry). The tie-ranking item of the K-U10 row (split_by_weights_into's exact remainders) is an owner decision and is not in this unit.

The findings

rust_decimal's checked_add and checked_mul return None only when the integer part overflows. When the exact result needs more than 96 bits of mantissa at the operands' scale, or a scale past 28, they return a rounded Some:

Operation Exact rust_decimal Found by
MAX + 0.4 79228162514264337593543950335.4 Some(MAX) K-U4
0.01 + 792281625142643375935439503.35 …503.36 …503.4 K-U8
1050000010.0000000001 x 9.9999999999 10500000099.89499999999999999999 10500000099.895000000000000000 K-U1
123456789.0123456789² 15241578753238836.75019051998750190521 15241578753238836.750190519988 K-U3
1e-28 x 1e-28 1e-56 Some(0) K-U1

checked_div (rounds at 28 significant digits) is the engine's rounding division and is unchanged.

A sixth, found in review of this unit: checked_div_exact ("never a rounded share") checked its quotient by multiplying back with rust_decimal's product. A rounded quotient times the divisor rounds back to the dividend (10 / 3 is 3.333…3 at 28 places, and 3.333…3 x 3 is held as 10), so 10 / 3, 10 / 7, 80 / 9 and 1000000.00 / 3 each came back Some. Its one caller divides a basis-point product by 10,000, which is always exact, so no figure was wrong. It now multiplies back with exact_mul and refuses them.

The method

impl Amount for Decimal (kernel/entries.rs) runs rust_decimal's operation and then checks, in 256-bit integers, that the figure it returned equals the exact result as a rational; if not, None. It never builds a figure of its own, so:

  1. Some(r) is the exact sum or product (checked, not assumed).
  2. A result that was exact is returned as rust_decimal gave it, scale and all: nothing exact moves, by construction.
  3. A result that was rounded is None; one that was None stays None.

The check (decimal_exact.rs): a Decimal is m x 10^-s, |m| < 2^96, s <= 28. For a sum a + b = r, at the common scale s = max(sa, sb, sr) every term m x 10^(s-si) is under 2^96 x 10^28 < 2^190, so the equation is Σ positive terms == Σ negative terms over unsigned 256-bit values, with no overflow possible. For a product, ma x mb (under 2^192, at scale sa + sb <= 56) is compared with mr at scale sr by scaling the side with fewer places up; overflow past 256 bits means that side is the larger, so unequal. Zero and sign are handled first. Any failure to compute answers "not exact".

Why not "refuse a sum whose result scale fell below the operands'". The K-U10 row's test is sound for a rounding but not complete as an exactness test in the other direction: it would refuse exact figures (a product whose 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, 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; this keeps the old inherent subtraction's 0, which is also what Money::checked_sub had (Money::checked_add and checked_sub now go through Amount, so they inherit the fix).

Two products that must keep rounding or round once

Making checked_mul exact would have refused posted-range inputs, found by running the suite (spec_cash_flow: a_shared_government_payment_splits_to_the_cent):

  • Shares (share_of, share_floor_with, so split_by_weights_with) multiply a cent amount by a percent of 27 places (33.333333333333333333333333333), whose exact product needs 29 places; then divide, which rounds anyway. They use a new Amount::checked_mul_rounded (rust_decimal's own product; on integers checked_mul), so every share is the figure it always was, to the digit.
  • A foreign amount at a rate (fx_booked) is not rounded early: Amount::checked_mul_round_to_step holds the exact product in 256 bits, cuts it only as far as Decimal needs while remembering whether any cut digit was non-zero (a sticky digit, kept below at least one real digit past the step), and rounds once with round_step. The sticky digit decides a tie (a half followed by zeros) and the directed modes as the full product would. Consequence: 1050000010.0000000001 x 9.9999999999 books 10500000099.89, the exact product's cent, where it booked 10500000099.90 through a rounded 10500000099.895. This is the one posted behaviour that changes (the D-75 ten-place bound alone does not make the product fit 96 bits, past about 7.9e8). None only for a figure too large to hold with its rounding digits (about 7.9e24 at a cent, 10^9 times a journal line's cap).

Everywhere else a product is the amount itself (line_amount, deductible_amount, percent_of, the gold/no-minor-unit FX product) and uses checked_mul: exact or None, each caller turning None into its existing refusal.

Kani

None added. Kani works on i64, which never rounds; Decimal is out of its reach (one symbolic Decimal add runs for more than ten minutes), so the claim rests on the bridge properties below and on Lean's laws over exact rationals, which these results now equal. The traceability gate's EXCEPTIONS is unchanged: the new items are pub(super) helpers and trait methods, not kernel functions.

Bridge properties (tests/kernel_bridge/decimal_exact.rs)

Compared against rust_decimal's own result (the old behaviour) and an independent num_rational::BigRational reference, over every mantissa to 2^96 - 1 (small, near the top, trailing zeros) and every scale 0 to 28, at 30,000 cases each:

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; 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, 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 (and, in fx.rs, the_mantissa_edge_books_the_exact_products_cent), and four unit tests inside decimal_exact.rs that check the private 256-bit helpers (mul_wide, checked_add, checked_mul_small, div_rem_10, scale_up) against num_bigint with edge values.

Canaries flipped

Was Is
total::finding_decimal_add_rounds_dust_at_the_edge total::a_sum_past_exact_arithmetic_is_refused
control::finding_decimal_add_rounds_scale_near_the_top control::a_sum_past_exact_arithmetic_is_refused
fx::the_mantissa_edge_is_outside_the_precondition_and_pinned fx::the_mantissa_edge_books_the_exact_products_cent
(new, K-U1/K-U3 products) decimal_exact::a_product_past_exact_arithmetic_is_refused
(new) decimal_exact::a_sum_past_exact_arithmetic_is_refused_at_the_maximum, …_near_the_top

Tests whose expectation moved (all outside what a posted line carries; each a rounded figure that is now refused or correctly rounded):

  • fx::past_ten_places_… now books 0.00 (the exact product's cent) where it booked 0.01; the engine still refuses the input (D-75).
  • schedule::the_edges: 599 parts of 1.3e26 at the cent sum past 96 bits at scale 2; rust_decimal rounded that sum, the kernel refuses it (and EXACT_LIMIT keeps the callers out of it).
  • control::kernel_matches_the_replaced_chains and decimal_matches_i128_reference now compare with each step's rounded results refused.

What did not change

  • checked_div, the engine's rounding division.
  • Every golden and every existing test outside the list above.
  • Direct calls of the inherent Decimal::checked_add/_sub/_mul outside the kernel (about 70 sites in reports, tax_capital, assets, arap, periods, schedules, …) still round at the 96-bit edge: not converted, 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.