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

double, equality, java, nan

Solution

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.

Problem

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

Original source