aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Output of this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Output of this code" Watch "Output of this code" New topic
Author

Output of this code

Swati Kadam
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: 3018
    
  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