Why does this comparison give me 'false'? I looked at the source and Float.NaN is defined as
/** * A constant holding a Not-a-Number (NaN) value of type * <code>float</code>. It is equivalent to the value returned by * <code>Float.intBitsToFloat(0x7fc00000)</code>. */ public static final float NaN = 0.0f / 0.0f;
EDIT: surprisingly, if I do this:
System.out.println("FC " + (Float.compare(Float.NaN, Float.NaN)));
it gives me 0
. So Float.compare()
does think that NaN is equal to itself!
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis.
NaN cannot be compared with any floating type value. This means that we'll get false for all comparison operations involving NaN (except “!= ” for which we get true). Hence, we cannot check for NaN by comparing with NaN using “==” or “!=
The Float. isNaN() method in Float Class is a built in method in Java returns true if this Float value or the specified float value is Not-a-Number (NaN), or false otherwise. Syntax: public boolean isNaN() or public static boolean isNaN(float val)
public static final double NaN. A constant holding a Not-a-Number (NaN) value of type double . It is equivalent to the value returned by Double.
Use Float.isNaN
to check for NaN values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With