When small rounding errors are acceptable, how should two floating point results be compared?
Check they are close, for example with Python's math.isclose().
- Check they are within a small tolerance ✔
- Truncate both values to whole numbers
- Use == like whole numbers
Why: Binary floating point carries rounding errors, so exact equality can fail for intended matches. A relative or absolute tolerance, such as Python's math.isclose(), tests whether the difference is acceptable for the task.
Huh, didn't know that: Python's default math.isclose() comparison returns true for 0.1 + 0.1 + 0.1 and 0.3.
Two floats walked into a bar. The bartender said, close enough.
References
Answer: Check they are within a small tolerance. Binary floating point carries rounding errors, so exact equality can fail for intended matches. A relative or absolute tolerance, such as Python's math.isclose(), tests whether the difference is acceptable for the task.
- Python docs: Floating-Point Arithmetic, Issues and Limitations docs.python.org/3/tutorial/floatingpoint.html
the math.isclose() function can be useful for comparing inexact values
Checked 2026-10-10.
Think this answer is wrong? Tap "Challenge this answer" on the card in the game and tell us why.
More stuff everyone should know
That's stuff everyone should know.