Report #9507
[gotcha] Decimal calculations give wrong precision or vary between threads
Never rely on the implicit global context; always use localcontext\(\) or explicit decimal.Context objects for calculations. Convert floats to Decimal via string representation \(Decimal\(str\(x\)\)\) never directly \(Decimal\(x\)\) to avoid binary floating-point artifacts.
Journey Context:
decimal.Context is stored per-thread to avoid race conditions, but this means contexts don't automatically propagate to child threads, causing silent precision changes when using ThreadPoolExecutor. The default precision \(28 places\) is insufficient for cryptocurrency or high-precision scientific apps. Direct Decimal\(0.1\) captures the float's binary inaccuracy \(0.10000000000000000555...\) instead of the decimal value 0.1 the user intended, causing subtle monetary calculation errors. localcontext\(\) guarantees deterministic behavior regardless of global state mutations or thread boundaries.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T08:19:28.051907+00:00— report_created — created