pub fn checked_div_python_scale(a: Decimal, b: Decimal) -> Option<Decimal>Expand description
Divide with Python decimal’s scale rule.
Python defines an ideal exponent for division: exp(a) - exp(b), i.e.
an ideal SCALE of scale(a) - scale(b). An exact quotient is reduced
toward that scale but never below the scale exactness requires:
0.00 / 4 -> 0.00 ideal 2, exact needs 0 -> 2
7 / 2 -> 3.5 ideal 0, exact needs 1 -> 1
1.00 / 2 -> 0.50 ideal 2, exact needs 1 -> 2
1.000 / 8 -> 0.125 ideal 3, exact needs 3 -> 3rust_decimal misses this in BOTH directions, so a pad-only fix would be
half a fix:
- Under, on a zero dividend — the same zero shortcut behind
add_python_scale.0.00 / 4gives0, dropping the scale. - Over, on an exact quotient —
7 / 2gives3.50, one trailing zero more than the ideal exponent allows.
So the quotient is stripped to its minimal form and then padded up to the
ideal scale; the two steps together land on Python’s answer from either
side. An inexact quotient (1 / 3) has no trailing zeros to strip and a
scale far past the ideal, so both steps are no-ops and it is returned as
rust_decimal computed it.
Returns None on divide-by-zero or overflow, matching
Decimal::checked_div — BQL maps that to NULL rather than panicking.