Why does Double.NaN==Double.NaN return false?
Solution 1
NaN means "Not a Number".
Java Language Specification (JLS) Third Edition says:
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 returnstrue
, includingx!=x
whenx
is NaN.
Solution 2
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.
http://en.wikipedia.org/wiki/NaN
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.
Solution 3
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
through0x7fffffffffffffffL
or in the range0xfff0000000000001L
through0xffffffffffffffffL
, the result is aNaN
.
Also, NaN
is logically treated inside the API.
Documentation
/**
* 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;
By the way, NaN
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);
}
Solution
What you can do is use compare
/compareTo
:
Double.NaN
is considered by this method to be equal to itself and greater than all otherdouble
values (includingDouble.POSITIVE_INFINITY
).
Double.compare(Double.NaN, Double.NaN);
Double.NaN.compareTo(Double.NaN);
Or, equals
:
If
this
andargument
both representDouble.NaN
, then theequals
method returnstrue
, even thoughDouble.NaN==Double.NaN
has the valuefalse
.
Double.NaN.equals(Double.NaN);
Solution 4
It might not be a direct answer to the question.
But if you want to check if something is equal to Double.NaN
you should use this:
double d = Double.NaN
Double.isNaN(d);
This will return true
Solution 5
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 byDouble.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.
Related videos on Youtube
Maverick
I am Software Engineer working specifically in Android. I have more than 4 years of experience in android and have created around 10 Apps for the clients I was engaged in. I have also created 3 free Android Apps and have published it on google playstore: https://play.google.com/store/apps/details?id=com.app.ais&hl=en https://play.google.com/store/apps/details?id=com.webplusandroid.akbarbirbal https://play.google.com/store/apps/details?id=com.webplusandroid.indianews I like to answer questions or doubts in android as much as I know and also try to learn new features and implementations. http://www.webplusandroid.com/
Updated on July 10, 2022Comments
-
Maverick almost 2 years
I was just studying OCPJP questions and I found this strange code:
public static void main(String a[]) { System.out.println(Double.NaN==Double.NaN); System.out.println(Double.NaN!=Double.NaN); }
When I ran the code, I got:
false true
How is the output
false
when we're comparing two things that look the same as each other? What doesNaN
mean?-
Stephan over 12 yearsThis is really weird. Because Double.NaN is static final, the comparision with == should return true. +1 for the question.
-
tdc over 12 yearsThe same is true in python:
In [1]: NaN==NaN Out[1]: False
-
zzzzBov over 12 yearsThe same is true in all languages that correctly follow the IEEE 754 standard.
-
Kevin over 12 yearsIntuition: "Hello" is not a number, true (boolean) is also not a number. NaN != NaN for the same reason "Hello" != true
-
Maverick over 12 years@kevin But when I am doing Double.compare(Double.NaN, Double.NaN) I am getting 0 as output i.e both are equal
-
sleske over 12 years@Stephan: The comparison with
Double.NaN==Double.NaN
should indeed return true ifDouble.NaN
were of typejava.lang.Double
. However, its type is the primitivedouble
, and the operator rules fordouble
apply (which demand this inequality for conformance with IEEE 754, as explained in the answers). -
sleske over 12 years@RaviKumar: Yes, that is indeed a bit inconsistent. However, it's explicitly documented in the Javadocs: "Double.NaN is considered by this method to be equal to itself " (see file:///C:/Users/sle/Downloads/Docs/jdk-6u25-fcs-bin-b04-apidocs-04_Apr_2011/docs/api/java/lang/Double.html#compareTo%28java.lang.Double%29 )
-
Pops over 12 yearsSee also Why is undefined == undefined but NaN != NaN? and is NaN equals to NaN (these are about JS, but as noted both above and below, this is language-independent).
-
Duncan Armstrong about 10 years@Kevin but:
String h = "Hello"; // not a number
assertFalse(h != h); // is false
Double.NaN behaviour certainly ain't intuitive...
-
-
Naweed Chougle over 12 yearsIs it hard wired in the JVM, or is it implemented by the CPU as Peter mentions?
-
Naweed Chougle over 12 yearsIs it implemented by the CPU, or is it hard-wired in the JVM as Bohemian mentions?
-
Vishy over 12 yearsThe 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)
-
ANeves over 12 years-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 as2+NaN
:an operation that has no mathematically definite result produces NaN
, as per the answer by @AdrianMitev -
Matteo over 12 yearsIndeed, 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).
-
Guillaume over 12 yearsAlso, this is in the Java source for Double: public static final double NaN = 0.0d / 0.0;
-
ANeves over 12 years@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 over 12 years@Guillaume if that comment was meant for me, please rephrase it: I do not understand it.
-
Matteo over 12 years@ANeves I agree that the goal is to give the best answer to the question. However, I think that there are several ways to explain the same thing. Additionally, I think that beginners can exploit a simple example of the phenomenon, and then they can re-elaborate on that.
-
Drew Dormann over 12 years@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
. -
supercat over 10 yearsDo you know of any case where having
NaN != NaN
be false would make programs more complicated than havingNaN != 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. -
falsarella over 10 years@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/…
-
supercat over 10 years@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 ay=f[n](x)
for somen
such that the result off(y)
is indistinguishable fromy
, theny
will be indistinguishable from the result of any more-deeply-nestedf(f(f(...f(y)))
. Even if one wantedNaN==NaN
to be false, havingNan!=Nan
also be false would be less "surprising" than havingx!=x
be true for some x. -
falsarella over 10 years@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.
-
supercat over 10 years@falsarella: I believe the type of
Double.NaN
is notDouble
, butdouble
, so the question is one related to the behavior ofdouble
. Though there exist functions which can test for an equivalence relation involvingdouble
values, the only compelling answer I know of for "why" (which is part of the original question) is "because some people at the IEEE didn't think equality-testing should define an equivalence relation". BTW, is there any concise idiomatic way to testx
andy
for equivalence using only primitive-operators? All formulations I know of are rather clunky. -
GoogieK about 10 years@supercat maybe testing mathematical series for convergence? I think +/- inf would probably be used instead, but I can imagine a case where a series that diverges is represented using NaN, and two such series wouldn't necessarily be equal
-
user207421 about 9 yearsBack in the day when the 8087 was an option, the C library contained an FP emulator. Programs like the JVM wouldn't have to worry about it either way.
-
supercat about 9 years@GoogieK: If a mathematical sequence is expected to converge but an iteration yields NaN, repeated iterations aren't apt to do much good. I would think that having NaN values compare as equal would cause most algorithms to announce in such a case that the sequence converged to NaN, while having them compare as unequal would cause the program to iterate forever absent a deliberate exit-test for NaN.
-
falsarella about 9 years@supercat The documentation also says, for example, that one of the reasons that
equals
returnstrue
when comparing twoNaN
's, is that it will make hashtables to work properly. -
supercat about 9 years@falsarella: The
equals
method in Java tests ifx
is greater than y, or if it's less than y, and if neither condition applies it converts the 64 bits of each value to along
with the same pattern and examines the resulting values. Not particularly concise. If one accepts positive and negative zero as equivalent (IMHO, they shouldn't be, but a definition of equality which regarded them as equivalent would at least be an equivalence relation),(x >= y && x <= y) || (x != x && y != y)
would probably be faster unless a particular Java implementation used a native implementation. -
falsarella about 9 years@supercat I agree: it's faster. I just wanted to add that the behaviors are different.
-
Supervisor over 8 yearsOpening up this Pandora's box - where do you see that "IEEE demands that (NAN != NAN) == false"?
-
Tarun Nagpal almost 8 yearsbest and simple answer. Thank you
-
user1803551 over 6 years
Double.NaN.equals/compareTo
is not valid code!Double.NaN
is adouble
and you can't invoke method on it. -
antak almost 4 years
Double.NaN.equals(Double.NaN)
: Did you meanDouble.valueOf(...).equals(Double.valueOf(...))
? -
falsarella almost 4 yearsThat's the way. The Double instance equality will consider NaNs as equal.