This week's book giveaway is in the OCAJP 8 forum.We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

# Output of this code

Ranch Hand

Joined: Mar 05, 2008
Posts: 39
System.out.println(Double.NaN == Double.NaN );
It prints False...
Why is it so?

SCJP / Preparing For SCWCD
Sandip Kaviman
Ranch Hand

Joined: Apr 27, 2004
Posts: 49
Because NaN is not equal anything even to itself also(as per K&B).

but still this does not answer why it is not equal to itself

Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
Originally posted by Sandip K:
Because NaN is not equal anything even to itself also(as per K&B).

but still this does not answer why it is not equal to itself

It is not equal to itselt, so you can find out that it is a NaN:
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028

10
You can also use Double.isNaN() to find out if a double is a NaN. Similarly Float.isNaN() for floats. That might be slightly clearer to programmers who forget about the existence of NaN.

Swati, Sandip - the reason NaN != NaN is by design. What should happen here?

Here two different calculations have the result NaN - should they be equal to each other? Probably not. They're both NaN, but they're different. Even if both x and y were 1.0/ 0.0, the word "equals" doesn't really have any clear meaning when you're talking about a number that is not a number. Equal to what? It's a special case, and Java has special rules for it. So do several other languages, since the idea of NaN existed before Java.

I agree. Here's the link: http://aspose.com/file-tools

subject: Output of this code