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:
1 parent
786e503858
commit
e46fa4fc6e
3 files changed
+71
-10
No files matched your search
@@ -376,7 +376,8 @@ proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(30000))]
|
||||
|
||||
/// `checked_mul_round_to_step` is the exact product rounded once, in
|
||||
/// every mode, at the steps the books use (one, a cent, a mill),
|
||||
/// every mode, at the steps the books use (one, a cent, a mill) and
|
||||
/// at steps of 5 and 25 of them,
|
||||
/// whether or not the exact product fits 96 bits, ties and signs
|
||||
/// included; `None` only for a figure past about 10^23 (the real digits, the
|
||||
/// sticky digit and the step's places must fit 96 bits).
|
||||
@@ -385,15 +386,17 @@ proptest! {
|
||||
foreign in arb_ten_place(26),
|
||||
rate in arb_ten_place(14),
|
||||
places in 0u32..=3,
|
||||
unit in prop_oneof![Just(1i128), Just(5i128), Just(25i128)],
|
||||
mode in arb_mode(),
|
||||
negative in any::<bool>(),
|
||||
) {
|
||||
let foreign = if negative { -foreign } else { foreign };
|
||||
let step = Decimal::from_i128_with_scale(1, places);
|
||||
// A step that is not a power of ten too (a nickel, 0.025): its
|
||||
// half steps sit one place below it, where the kept digit is real.
|
||||
let step = Decimal::from_i128_with_scale(unit, places);
|
||||
let got = Amount::checked_mul_round_to_step(foreign, rate, step, mode);
|
||||
let scale = BigRational::from_integer(BigInt::from(10u8).pow(places));
|
||||
let count = round_rational(&(rat(foreign) * rat(rate) * scale.clone()), mode);
|
||||
let want = BigRational::from_integer(count) / scale;
|
||||
let count = round_rational(&(rat(foreign) * rat(rate) / rat(step)), mode);
|
||||
let want = BigRational::from_integer(count) * rat(step);
|
||||
match got {
|
||||
Some(g) => prop_assert_eq!(rat(g), want, "{} x {} to {} places, {:?}", foreign, rate, places, mode),
|
||||
None => prop_assert!(
|
||||
@@ -447,3 +450,48 @@ fn the_k_u1_edge_books_the_cent_the_exact_product_gives() {
|
||||
Some("1000000000.10".parse().unwrap())
|
||||
);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------ exact division
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(30000))]
|
||||
|
||||
/// `checked_div_exact` is the exact quotient or `None`: never a
|
||||
/// quotient `rust_decimal` rounded (10 / 3), whose product with the
|
||||
/// divisor rounds back to the dividend.
|
||||
#[test]
|
||||
fn div_exact_is_exact_or_none(a in arb_line_amount(), m in 1i128..100_000, s in 0u32..=4) {
|
||||
let b = Decimal::from_i128_with_scale(m, s);
|
||||
if let Some(q) = Amount::checked_div_exact(a, b) {
|
||||
prop_assert_eq!(rat(q) * rat(b), rat(a), "{} / {} gave {}", a, b, q);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A third, a seventh, a ninth: no exact quotient, so `None`, where the
|
||||
/// check through `rust_decimal`'s rounding product let each through. A
|
||||
/// quarter and a basis-point division are exact and are kept.
|
||||
#[test]
|
||||
fn a_rounded_quotient_is_not_an_exact_one() {
|
||||
let d = |s: &str| s.parse::<Decimal>().unwrap();
|
||||
for (a, b) in [
|
||||
("10", "3"),
|
||||
("20", "3"),
|
||||
("10", "7"),
|
||||
("80", "9"),
|
||||
("10", "6"),
|
||||
("1000000.00", "3"),
|
||||
] {
|
||||
assert_eq!(
|
||||
Amount::checked_div_exact(d(a), d(b)),
|
||||
None,
|
||||
"{a} / {b} has no exact quotient"
|
||||
);
|
||||
}
|
||||
assert_eq!(Amount::checked_div_exact(d("10"), d("4")), Some(d("2.5")));
|
||||
assert_eq!(
|
||||
Amount::checked_div_exact(d("123456.78") * d("250"), d("10000")),
|
||||
Some(d("3086.4195"))
|
||||
);
|
||||
assert_eq!(Amount::checked_div_exact(d("1"), d("0")), None);
|
||||
}
|
||||
Reference in new issue
Block a user