It's OK to compare floating-points for equality
4 days ago
- #numerical-analysis
- #floating-point
- #programming
- Floating-point epsilon comparisons are often problematic because they are arbitrary, lack transitivity, and can lead to inconsistent program behavior.
- Epsilon comparisons are usually unnecessary; better solutions exist, such as using fixed-point numbers, integer arithmetic, or exact comparisons (x == y).
- Examples demonstrate that epsilons are not the best solution in cases like game unit movement, slerp, vector length, solving linear systems, ray-box intersection, and geometric predicates.
- There are specific cases where epsilon comparisons are acceptable, such as in visualization for filtering close points or in testing mathematical libraries.
- The key takeaway is to think critically about why floating-point comparisons are needed and to choose appropriate solutions based on the problem context, rather than defaulting to epsilons.