How do you test to see if a double is equal to NaN?

JavaDoubleEqualityNan

Java Problem Overview


I have a double in Java and I want to check if it is NaN. What is the best way to do this?

Java Solutions


Solution 1 - Java

Use the static Double.isNaN(double) method, or your Double's .isNaN() method.

// 1. static method
if (Double.isNaN(doubleValue)) {
    ...
}
// 2. object's method
if (doubleObject.isNaN()) {
    ...
}

Simply doing:

if (var == Double.NaN) {
    ...
}

is not sufficient due to how the IEEE standard for NaN and floating point numbers is defined.

Solution 2 - Java

Try Double.isNaN():

> Returns true if this Double value is a Not-a-Number (NaN), false otherwise.

Note that [double.isNaN()] will not work, because unboxed doubles do not have methods associated with them.

Solution 3 - Java

You might want to consider also checking if a value is finite via Double.isFinite(value). Since Java 8 there is a new method in Double class where you can check at once if a value is not NaN and infinity.

/**
 * Returns {@code true} if the argument is a finite floating-point
 * value; returns {@code false} otherwise (for NaN and infinity
 * arguments).
 *
 * @param d the {@code double} value to be tested
 * @return {@code true} if the argument is a finite
 * floating-point value, {@code false} otherwise.
 * @since 1.8
 */
public static boolean isFinite(double d)

Solution 4 - Java

You can check for NaN by using var != var. NaN does not equal NaN.

EDIT: This is probably by far the worst method. It's confusing, terrible for readability, and overall bad practice.

Solution 5 - Java

If your value under test is a Double (not a primitive) and might be null (which is obviously not a number too), then you should use the following term:

(value==null || Double.isNaN(value))

Since isNaN() wants a primitive (rather than boxing any primitive double to a Double), passing a null value (which can't be unboxed to a Double) will result in an exception instead of the expected false.

Solution 6 - Java

The below code snippet will help evaluate primitive type holding NaN.

double dbl = Double.NaN; Double.valueOf(dbl).isNaN() ? true : false;

Solution 7 - Java

Beginners needs practical examples. so try the following code.

public class Not_a_Number {

public static void main(String[] args) {
    String message = "0.0/0.0 is NaN.\nsimilarly Math.sqrt(-1) is NaN.";        
    String dottedLine = "------------------------------------------------";     

    Double numerator = -2.0;
    Double denominator = -2.0;      
    while (denominator <= 1) {
        Double x = numerator/denominator;           
        Double y = new Double (x);
        boolean z = y.isNaN();
        System.out.println("y =  " + y);
        System.out.println("z =  " + z);
        if (z == true){
            System.out.println(message);                
        }
        else {
            System.out.println("Hi, everyone"); 
        }
        numerator = numerator + 1;
        denominator = denominator +1;
        System.out.println(dottedLine);         
    } // end of while

} // end of main

} // end of class

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric WilsonView Question on Stackoverflow
Solution 1 - JavaBen SView Answer on Stackoverflow
Solution 2 - JavaAndrew HareView Answer on Stackoverflow
Solution 3 - JavaGrzegorz GajosView Answer on Stackoverflow
Solution 4 - Javahyper-neutrinoView Answer on Stackoverflow
Solution 5 - JavaPeterPainterView Answer on Stackoverflow
Solution 6 - JavaTeelaView Answer on Stackoverflow
Solution 7 - Javap.g.gajendra babuView Answer on Stackoverflow