Skip to main content

Module decimal

Module decimal 

Source
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 — see checked_add_python_scale.
negate_python
Negate with Python decimal’s sign rule for zero.
round_dp_python
Round to dp decimal places with Python decimal’s sign rule for zero.
sub_python_scale
Subtract with Python decimal’s scale rule — see add_python_scale.