How do you test to see if a double is equal to NaN?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
You do not test whether a floating-point value is NaN with normal equality, because NaN is defined to be unequal to every value, including itself. The correct approach is to use the language’s dedicated NaN-checking function or method, which follows IEEE 754 floating-point rules.
Core Sections
Why x == NaN never works
NaN means “not a number,” and under IEEE 754 rules it is special: it is not equal to any number and it is not equal to another NaN either.
In Java, this means the following comparison is always false:
That surprises many developers at first, but it is expected floating-point behavior.
The right Java answer: Double.isNaN()
In Java, the standard and readable solution is Double.isNaN(value).
This is the idiomatic test in Java code. It states intent clearly and avoids relying on floating-point tricks.
The self-inequality trick exists, but it is not the best style
Because NaN is the only floating-point value that is not equal to itself, this expression works too:
Technically, this identifies NaN. Practically, it is less readable than Double.isNaN(value). It is more of an IEEE-754 fact than a good code style recommendation.
NaN can propagate through calculations
NaN often appears after invalid floating-point operations, and once it enters a calculation, it tends to propagate.
That is why NaN handling matters in numeric code, parsing pipelines, analytics, and scientific computation. One bad value can quietly contaminate later results.
Related checks: infinity is different from NaN
Do not confuse NaN with positive or negative infinity. They are all special floating-point values, but they mean different things.
If your numeric validation logic cares about all non-finite values, you may need both checks.
Other languages follow the same idea with different APIs
The rule is cross-language even though the function name varies.
So the deeper lesson is not just “which function do I call.” It is “NaN must be checked with a dedicated predicate rather than normal equality.”
Common Pitfalls
- Comparing directly with
== Double.NaNalways fails because NaN is not equal to anything, including itself. - Forgetting that NaN propagates through later arithmetic can hide the original source of bad numeric data.
- Confusing NaN with infinity leads to incomplete validation logic when non-finite values must be handled explicitly.
- Using the self-inequality trick in production code reduces readability compared with
Double.isNaN(). - Assuming all floating-point problems are precision issues ignores that NaN is a special value-class issue, not just rounding error.
Summary
- Do not test NaN with ordinary equality.
- In Java, use
Double.isNaN(value)for a clear and correct check. - NaN is unique because it is not equal to itself.
- Infinity and NaN are different special floating-point cases.
- When NaN appears, it often propagates, so early detection improves numeric debugging.

