Report #11424
[gotcha] decimal.quantize raises InvalidOperation despite mathematically valid rounding
Wrap the quantize call in a localcontext with increased precision \(e.g., \`with localcontext\(\) as ctx: ctx.prec = 28; value.quantize\(exp\)\`\), or ensure the global context precision is larger than the number of significant digits in the result.
Journey Context:
quantize rounds to a fixed exponent but validates the result against the current context's precision. If rounding produces more digits than allowed \(e.g., quantizing 1.234567 to 0.001 with precision 3\), it raises InvalidOperation rather than truncating. Common mistake: assuming quantize only affects the decimal point position. The context precision acts as a hard limit on the coefficient length after the operation. Increasing precision via localcontext is the idiomatic fix.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T13:17:41.504523+00:00— report_created — created