Expand description
Decimal arithmetic with Python decimal scale semantics.
rust_decimal and Python’s decimal agree on the VALUE of a sum but not
always on its SCALE, and BQL renders a naked decimal at its intrinsic scale
(bean-query’s DecimalRenderer pads for alignment only — it never
quantizes). So a scale difference is a visible output difference.
Python’s rule for +/- is that an exact result carries
max(scale(a), scale(b)):
Decimal("0.00") + Decimal("1") == Decimal("1.00")rust_decimal matches that for ordinary operands (2.00 + 1 == 3.00) but
not when one side is ZERO: its addition returns the other operand
unchanged, so the zero’s scale is discarded and 0.00 + 1 == 1.
That makes an accumulation order-dependent, which is how it surfaced. A
SUM over -1, 1, -111.11, 111.11, -2, 2 passes through 0.00 at the
fourth term; the -2 that follows then resets the running total to scale
0 and the query prints 0 where bean-query prints 0.00. Move the
fractional pair last and the same multiset prints 0.00 — same value,
same inputs, different rendering.
Functions§
- add_
python_ scale - Add two decimals with Python
decimal’s scale rule. - checked_
add_ python_ scale - Overflow-checked
add_python_scale. - checked_
div_ python_ scale - Divide with Python
decimal’s scale rule. - checked_
sub_ python_ scale - Overflow-checked
sub_python_scale— seechecked_add_python_scale. - negate_
python - Negate with Python
decimal’s sign rule for zero. - round_
dp_ python - Round to
dpdecimal places with Pythondecimal’s sign rule for zero. - sub_
python_ scale - Subtract with Python
decimal’s scale rule — seeadd_python_scale.