Take the 2-minute tour ×

I was just studying OCPJP questions and I found this strange code:

public static void main(String a[]) {

When I ran the code, I got:


How is the output false when we're comparing two things that look the same as each other? What does NaN mean?

share|improve this question
This is really weird. Because Double.NaN is static final, the comparision with == should return true. +1 for the question. –  Stephan Jan 11 '12 at 14:10
The same is true in python: In [1]: NaN==NaN Out[1]: False –  tdc Jan 11 '12 at 15:29
The same is true in all languages that correctly follow the IEEE 754 standard. –  zzzzBov Jan 11 '12 at 16:13
Intuition: "Hello" is not a number, true (boolean) is also not a number. NaN != NaN for the same reason "Hello" != true –  Kevin Jan 12 '12 at 23:01
@kevin But when I am doing Double.compare(Double.NaN, Double.NaN) I am getting 0 as output i.e both are equal –  Maverick Jan 13 '12 at 4:09

7 Answers 7

up vote 92 down vote accepted

NaN means "Not a Number".

Java Language Specification (JLS) says:

Floating-point operators produce no exceptions (§11). An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a result. As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN.

share|improve this answer
It is not just Java; it is in the floating point standard. –  nibot Jan 11 '12 at 16:46
@nibot: Mostly true. Any comparison with an IEEE-conforming float will produce false. So that standard differs from Java in that IEEE demands that (NAN != NAN) == false. –  Drew Dormann Jan 17 '12 at 19:57

NaN is by definition not equal to any number including NaN. This is part of the IEEE 754 standard and implemented by the CPU/FPU. It is not something the JVM has to add any logic to support.


A comparison with a NaN always returns an unordered result even when comparing with itself. ... The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN.

Java treats all NaN as quiet NaN.

share|improve this answer
Is it implemented by the CPU, or is it hard-wired in the JVM as Bohemian mentions? –  Enthusiast Jan 11 '12 at 16:56
The JVM has to call whatever will implement it correctly. On a PC, the CPU does all the work as such. On a machine without this support the JVM has to implement it. (I don't know of any such machine) –  Peter Lawrey Jan 11 '12 at 17:05

Why that logic

NaN means Not a Number. What is not a number? Anything. You can have anything in one side and anything in the other side, so nothing guarantees that both are equals. NaN is calculated with Double.longBitsToDouble(0x7ff8000000000000L) and as you can see in the documentation of longBitsToDouble:

If the argument is any value in the range 0x7ff0000000000001L through 0x7fffffffffffffffL or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the result is a NaN.

Also, NaN is logically treated inside the API.

More info


 * A constant holding a Not-a-Number (NaN) value of type
 * {@code double}. It is equivalent to the value returned by
 * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
public static final double NaN = 0.0d / 0.0;

Also, by the way, it is tested as your code sample:

 * Returns {@code true} if the specified number is a
 * Not-a-Number (NaN) value, {@code false} otherwise.
 * @param   v   the value to be tested.
 * @return  {@code true} if the value of the argument is NaN;
 *          {@code false} otherwise.
static public boolean isNaN(double v) {
    return (v != v);


What you can do is use equals to compare:

Double.compare(Double.NaN, Double.NaN);

As documentation points for compare:

 *  {@code Double.NaN} is considered by this method
 *  to be equal to itself and greater than all other
 *  {@code double} values (including
 *  {@code Double.POSITIVE_INFINITY}).
share|improve this answer
+1 Very good answer! –  higuaro Jan 30 '13 at 12:57
Do you know of any case where having NaN != NaN be false would make programs more complicated than having NaN != NaN be true? I know IEEE made the decision ages ago, but from a practical perspective, I've never seen cases where it's useful. If an operation is supposed to run until consecutive iterations yield the same result, having two consecutive iterations yield NaN would be "naturally" detected as an exit condition were it not for that behavior. –  supercat Sep 7 '13 at 20:33
@supercat How can you say that two random non number are naturally equal? Or say, primitively equal? Think of NaN as an instance, not something primitive. Each different abnormal result is a different instance of something strange and even if both should represent the same, using == for different instances must return false. On the other hand, when using equals it can be handled properly as you intend. [docs.oracle.com/javase/7/docs/api/java/lang/… –  falsarella Sep 9 '13 at 11:52
@falsarella: The issue isn't whether two random numbers should be considered "definitely equal", but rather in what cases is it useful to have any number compare as "definitely unequal" to itself. If one is trying to compute the limit of f(f(f...f(x))), and one finds a y=f[n](x) for some n such that the result of f(y) is indistinguishable from y, then y will be indistinguishable from the result of any more-deeply-nested f(f(f(...f(y))). Even if one wanted NaN==NaN to be false, having Nan!=Nan also be false would be less "surprising" than having x!=x be true for some x. –  supercat Sep 11 '13 at 16:36
@supercat If you are playing with Double's (not double's) and you write a condition using == (which would be also strange not to use equals here), it means that you would be willing to execute the inner block when both sides are equal (obviously), and probably use tem (say, in an equation), so the less "surprising", for me, would be entering in my block when NaN = NaN and cause a surprisingly unexpected error! I don't have a showcase for you and probably just few people have passed through that problem. That is an OCPJP question and the point is to clarify and solve the OP's problem. –  falsarella Sep 11 '13 at 17:22

The javadoc for Double.NaN says it all:

A constant holding a Not-a-Number (NaN) value of type double. It is equivalent to the value returned by Double.longBitsToDouble(0x7ff8000000000000L).

Interestingly, the source for Double defines NaN thus:

public static final double NaN = 0.0d / 0.0;

The special behaviour you describe is hard-wired into the JVM.

share|improve this answer
Is it hard wired in the JVM, or is it implemented by the CPU as Peter mentions? –  Enthusiast Jan 11 '12 at 16:54

NaN is a special value that denotes "not a number"; it's the result of certain invalid arithmetic operations, such as sqrt(-1), and has the (sometimes annoying) property that NaN != NaN.

share|improve this answer

Not a number represents the result of operations whose result is not representable with a number. The most famous operation is 0/0, whose result is not known.

For this reason, NaN is not equal to anything (including other not-a-number values). For more info, just check the wikipedia page: http://en.wikipedia.org/wiki/NaN

share|improve this answer
-1: It does not represent the result of 0/0. 0/0 is always NaN, but NaN can be the result of other operations - such as 2+NaN: an operation that has no mathematically definite result produces NaN, as per the answer by @AdrianMitev –  ANeves Jan 11 '12 at 19:02
Indeed, NaN stands for "Not a Number", and it is the result of all operations that have as result an undefined or unrepresentable value. The most famous and common operation is 0/0, but obviously there are tons of other operations that has the same result. I agree that my answer could be improved, but I disagree on the -1... I just checked that also wikipedia uses the 0/0 operations as the first example of operation with an NaN result (en.wikipedia.org/wiki/NaN). –  Matteo Jan 12 '12 at 12:03
Also, this is in the Java source for Double: public static final double NaN = 0.0d / 0.0; –  Guillaume Jan 12 '12 at 12:13
@Matteo +0, now that the false statement is gone. And my -1 or +1 are not for you to agree or disagree; but it is good to leave a comment with a -1, so that the author can understand why his answer is considered unuseful - and change it, if he so wishes. –  ANeves Jan 12 '12 at 14:58
@Guillaume if that comment was meant for me, please rephrase it: I do not understand it. –  ANeves Jan 12 '12 at 14:59

There is a simple alternative to know if a double variable is a NaN:

private void boolean isNaN(double d) {
    return d != d;

If d is a normal double, for example, 7.553, it will always return true, and if d is a NaN, when it compares with itself, it will always return false =)

share|improve this answer

Your Answer


