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:
1 parent
e46fa4fc6e
commit
5777787b69
6 files changed
+210
-23
No files matched your search
@@ -9,7 +9,10 @@
|
||||
//! division, so a `Decimal` quotient is rounded at 28 significant digits
|
||||
//! (and a machine integer's is truncated), never an exact rational.
|
||||
//! Multiplication is not exact either at `Decimal`'s 96-bit mantissa:
|
||||
//! `checked_mul` rounds the product there. Every invariant below is
|
||||
//! `checked_mul_rounded` rounds the product there, and this module uses
|
||||
//! it by name wherever a rounded product is the figure meant (the
|
||||
//! kernel's `checked_mul` is exact or refused, K-U10, and would refuse a
|
||||
//! share by a percent of 27 places). Every invariant below is
|
||||
//! stated of that computation, and of the parts after the named leftover
|
||||
//! policy, which do sum to the whole exactly. See `docs/proofs/k-u3.md`
|
||||
//! for the edges where a product's or a quotient's rounding can change a
|
||||
|
||||
@@ -201,8 +201,14 @@ fn product_is_exact(a: Decimal, b: Decimal, product: Decimal) -> bool {
|
||||
/// rounded one. `None` when the product is too large to hold with those
|
||||
/// digits: the figure times 10^(step places + 2) must fit 96 bits, so a
|
||||
/// figure past about 7.9e26 at a whole-unit step, 7.9e24 at a cent (10^9
|
||||
/// times a journal line's cap). A zero product is exact and never gets
|
||||
/// here.
|
||||
/// times a journal line's cap). For the same reason a step of 27 or 28
|
||||
/// places is always `None` here: `Decimal` ends at 28 places, so there is
|
||||
/// no place below such a step for the real digit and the sticky one. That
|
||||
/// refuses some figures whose rounded result `Decimal` could hold (1e-28
|
||||
/// squared to a step of 1e-27 is zero); a refusal, never a guess, and no
|
||||
/// currency's step is within twenty places of it (pinned in the bridge:
|
||||
/// `a_step_of_27_places_refuses_a_product_that_is_not_exact`). A zero
|
||||
/// product is exact and never gets here.
|
||||
pub(super) fn mul_for_rounding(a: Decimal, b: Decimal, step_scale: u32) -> Option<Decimal> {
|
||||
let negative = (a.mantissa() < 0) != (b.mantissa() < 0);
|
||||
let mut digits = Wide::mul_wide(a.mantissa().unsigned_abs(), b.mantissa().unsigned_abs());
|
||||
|
||||
@@ -58,7 +58,12 @@ pub trait Amount: Copy + Ord {
|
||||
/// For [`Decimal`] the exact product may need more than 96 bits (ten
|
||||
/// places each, D-75, is twenty places, and past 7.9e8 the mantissa
|
||||
/// passes `Decimal`'s): it is held wider than `Decimal` for the one
|
||||
/// rounding, never rounded early and then again.
|
||||
/// rounding, never rounded early and then again. Two refusals come
|
||||
/// with that, both far from any books: a product that is not exact
|
||||
/// and is past about 7.9e26 divided by 10^(the step's places), and a
|
||||
/// product that is not exact at a step of 27 or 28 places (the
|
||||
/// rounding needs two places below the step, and `Decimal` ends at
|
||||
/// 28). An exact product is rounded at any step.
|
||||
fn checked_mul_round_to_step(self, rhs: Self, step: Self, mode: Rounding) -> Option<Self> {
|
||||
super::round::round_step(self.checked_mul(rhs)?, step, mode)
|
||||
}
|
||||
|
||||
@@ -31,8 +31,8 @@ use super::{Amount, Rounding, round_step, share_of};
|
||||
/// 1. Some(r) is round_step(part / whole × 100, step, mode), computed
|
||||
/// division first and then the multiplication, the order every call
|
||||
/// site used before U6. For Decimal the quotient is exact while it
|
||||
/// fits 28 significant digits and the product while it fits the
|
||||
/// 96-bit mantissa; either operation rounds at that edge (K-U10).
|
||||
/// fits 28 significant digits and rounds past them; the product by
|
||||
/// 100 is exact or refused, never rounded (K-U10).
|
||||
/// For a machine integer the division truncates toward zero.
|
||||
/// 2. None when whole <= 0, step <= 0, or a quotient, product or
|
||||
/// rounded result is not representable. Never a panic.
|
||||
|
||||
Reference in new issue
Block a user