fix(core): an exact division refuses a rounded quotient

`Amount::checked_div_exact` promises the exact quotient or nothing: "never
a rounded share". On Decimal it checked the quotient by multiplying back,
but with rust_decimal's own product, which rounds. 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 a third, a seventh and a
ninth all passed as exact.

No figure in the books was wrong: the one caller divides a basis-point
product by 10,000, which is always exact. The contract was broken for
the next caller. The check now multiplies back exactly (K-U10's
`exact_mul`), so a rounded quotient is refused.

The bridge gets the property (a quotient returned, times the divisor, is
the dividend as a rational) and the named cases. The FX rounding
property also takes steps of 5 and 25 units, whose half steps sit one
place below the step: the wide product keeps that digit real.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-09 21:05:43 -07:00
1 parent 786e503858
commit e46fa4fc6e
3 files changed
+71 -10

No files matched your search

+5 -1
View File
@@ -151,7 +151,11 @@ impl Amount for Decimal {
}
fn checked_div_exact(self, rhs: Self) -> Option<Self> {
let q = self.checked_div(rhs)?;
(q.checked_mul(rhs) == Some(self)).then_some(q)
// The exact product, not `rust_decimal`'s: a rounded quotient
// times the divisor rounds back to the dividend there (10 / 3 is
// 3.333...3, and 3.333...3 x 3 is held as 10), which would pass a
// rounded share as an exact one.
(super::decimal_exact::exact_mul(q, rhs) == Some(self)).then_some(q)
}
fn checked_div(self, rhs: Self) -> Option<Self> {
Decimal::checked_div(self, rhs)