Report #104273
[gotcha] Using \`in\` on a list of floats \(or Decimal\) for exact membership silently fails due to floating-point rounding
Never use \`x in list\_of\_floats\` for approximate equality. Instead, use \`math.isclose\(x, y\)\` with an explicit tolerance, or store values as Fraction/Decimal and check with a tolerance-aware helper. For sorted lists, use bisect with isclose.
Journey Context:
A common pattern: compute some float values, collect them in a list, then check if a new computed value is in that list. Because of IEEE 754 rounding, \`0.1 \+ 0.2 == 0.3\` is False, so \`0.3 in \[0.1 \+ 0.2\]\` is False. This is not a Python bug but a fundamental floating-point property. Developers often assume \`in\` uses \`==\` which uses exact bitwise comparison. The fix is to never rely on exact equality for floats. Use a tolerance or switch to Decimal with quantize. This bites especially in financial calculations, numerical simulation, and geometric algorithms.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-26T20:02:52.076347+00:00— report_created — created