In Java, when is a float equal to zero?

Wilco picture Wilco · Oct 16, 2013 · Viewed 23.3k times · Source

For what values of x does the test (x == 0) return true? Is there some kind of margin or does the test return true if and only if the value of x = 0?

Answer

JonAar Livernois picture JonAar Livernois · Apr 11, 2015

When Math.signum(x) == 0.

All other attempts to check whether float x == 0 may fail.

But Math.signum() is so basic, it should never fail.