Report #5987
[gotcha] Decimal arithmetic silently rounds to context precision causing precision loss
Use localcontext\(\) to set precision higher than your calculation requires for intermediate steps, then quantize\(\) to the exact exponent for final results. Never rely on default prec=28 for chained financial calculations.
Journey Context:
The decimal module uses a context with limited precision \(default 28 places\) for all operations. Unlike binary float, the issue isn't representation error but rounding to the context precision during every operation. For example, \(Decimal\('1'\) / Decimal\('3'\)\) \* Decimal\('3'\) yields 0.999... not 1, because the division rounds to 28 digits, then the multiplication rounds again. This breaks financial invariants. The fix is to use localcontext to increase precision for intermediate steps, then quantize to the required exponent for final results.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T22:46:37.615671+00:00— report_created — created