🤖 The Prompt That Catches == on Floating-Point Numbers
Comparing two floating-point values with == works right up until the moment two numbers that are mathematically equal end up represented with a tiny binary rounding difference – and then a test that passed a thousand times starts failing intermittently, or a running total that should match a displayed value is off by a fraction of a cent. These comparisons are easy to write without thinking twice and easy to miss in a normal code review, because the code reads as obviously correct.
🔎 The Prompt
Review the pasted code for direct equality comparisons (==, Equals, or language-equivalent) involving float, double, or decimal values (or their equivalents in this language) that were produced by a calculation rather than read as a fixed literal. For each one: 1. Quote the exact comparison and the line it's on. 2. Explain specifically why this comparison can fail even when the two values are mathematically equal - trace through where the rounding difference would actually come from in this specific calculation. 3. Suggest the correct fix for this case specifically: either an epsilon/tolerance-based comparison with a concrete suggested tolerance, or switching the underlying type to a fixed-point/decimal type if the values represent money or another exact quantity. 4. Separately list any comparisons against a fixed literal (checking if a value equals exactly 0 or 1, for example) that are actually fine as written, so I know what NOT to change. Code to review: [paste the code here]
✅ Fix: Why This Prompt Works
- Asking it to trace where the rounding difference would specifically come from, rather than giving a generic warning about floating point, forces a real answer tied to the actual calculation in front of you – that’s what separates a comparison that’s genuinely risky from one that happens to use a float type but is actually safe.
- Explicitly asking for the cases that are fine as written is the detail that keeps this prompt useful rather than alarming – a blanket ‘never compare floats with ==’ rule would flag plenty of harmless comparisons against sentinel values like 0 or -1 right alongside the real risks.
- Requesting a concrete suggested tolerance instead of a generic ‘use an epsilon’ gives you something you can actually paste into the fix, rather than a reminder you still have to go research the right magnitude for.
⚠️ Why This Is Easy to Miss
- This isn’t a lint rule you can just turn on everywhere – some floating-point equality checks against literal sentinel values are completely correct, and a reviewer (human or automated) that flags all of them equally just trains people to ignore the warning.
- For anything representing money specifically, the right fix usually isn’t a tolerance at all – it’s switching to a decimal or fixed-point type from the start, since even a well-chosen epsilon is a workaround for a type choice that shouldn’t have been float in the first place.
Two floating-point numbers that are mathematically equal aren’t always bit-for-bit equal – == doesn’t know the difference, and a rounding error that small is exactly the kind of thing worth having a second pass look for.
