Equality tests on floating point values may lead to unexpected results because of arithmetic imprecision. For example, the expression 23.42f==23.42 evaluates to false.

Instead of testing for exact equality between floating point values, check that the difference between the values is within an appropriate error margin.

Alternatively, if you do not want any inaccuracy when testing for equality, use one of the following instead of floating point values:

In the following example, (0.1 + 0.2) == 0.3 evaluates to false, even though you would expect it to evaluate to true. This is because of the imprecision of floating point data types.

In the following improved example, the test for equality is performed by calculating the difference between the two values, and checking if the difference is within the error margin, EPSILON.

  • J. Bloch, Effective Java (second edition), Item 48. Addison-Wesley, 2008.
  • Numerical Computation Guide: What Every Computer Scientist Should Know About Floating-Point Arithmetic.