What’s wrong with using == to compare floats in Java?

the correct way to test floats for ‘equality’ is:

if(Math.abs(sectionID - currentSectionID) < epsilon)

where epsilon is a very small number like 0.00000001, depending on the desired precision.

Leave a Comment