Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float.NaN == Float.NaN

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!

like image 275
shrini1000 Avatar asked Feb 18 '12 13:02

shrini1000


People also ask

What is NaN in float?

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.

How does NaN compare to float?

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 “!=

What is float NaN Java?

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)

What is a double NaN?

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.


1 Answers

Use Float.isNaN to check for NaN values.

like image 135
pencil Avatar answered Oct 05 '22 21:10

pencil