Explain Codes LogoExplain Codes Logo

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

java
prompt-engineering
best-practices
functions
Alex KataevbyAlex Kataev·Feb 14, 2025
TLDR

Use Double.isNaN(value) to check if a double is NaN. Direct comparisons with NaN such as value == Double.NaN will not work, since in the realm of floating point numbers, NaN is considered unequal to any value, including itself. Here's a quick example to illustrate this:

double value = Double.NaN; boolean isNan = Double.isNaN(value); // This will be true if value is NaN System.out.println("As per the matrix, the value is " + (isNan ? "NaN." : "not NaN."));

This method is built into the Java's Double class and provides a reliable check for NaN.

In-depth: Getting to know NaN

In Java, NaN stands for Not-a-Number. It's a special double value that represents the result of an undefined or unrepresentable mathematical operation.

The failed equality operation

Directly comparing a double value to Double.NaN always yields false. This is due to NaN's special property:

double nanValue = Double.NaN; System.out.println(nanValue == Double.NaN); // Prints false and breaks your heart 💔

The above operation fails because of the IEEE 754 floating-point standard, which states NaN is unequal to any value, even itself (NaN knows the importance of being unique).

NaN != NaN? Yes, indeed!

Consequently, a double value allows a rather strange operation where NaN is not equal (!=) to NaN. While this may appear incorrect, it correctly follows the IEEE 754 standard:

double nanValue = Double.NaN; System.out.println(nanValue != nanValue); // It's true!💥 Believe it or not

Using isNaN() with Double objects

When dealing with Double objects (the object form of the primitive double), you can use the object's isNaN() method:

Double obj = new Double(Double.NaN); boolean isNan = obj.isNaN(); // True for NaN objects

This is particularly useful when working with Double objects rather than primitive double.

Protecting against null and NaN

In situations where you're dealing with Double objects that could potentially be null or NaN, you can perform a compound check:

Double value = /* Double instance that might be null or NaN */; boolean isNullOrNan = (value == null || value.isNaN());

NaN in the wild: Use-cases and edge cases

Don't let NaN sneak into your calculations

NaN can result from certain mathematical operations and can propagate through your calculations:

double result = 0.0 / 0.0; // Results in NaN System.out.println(Double.isNaN(result)); // True, NaN spotted doing the limbo!

Testing for NaN in unit tests with JUnit

JUnit offers the assertEquals() method that accepts NaN as a valid argument:

assertEquals(Double.NaN, result, 0.0); // Passes if result is NaN

Remember, a good unit test is as clear as your future with clean code!

Dealing with NaN during comparisons

When dealing with comparisons involving NaN, consider using special tools like the Comparator interface in Java:

Comparator<Double> nanSafeComparator = Comparator.comparingDouble(Double::doubleValue);

This allows us to make NaN friendly comparisons (because apparently NaN has feelings too).

References