File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Double.NaN ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Double.NaN ???" Watch "Double.NaN ???" New topic

Double.NaN ???

lydia westland
Ranch Hand

Joined: Feb 24, 2002
Posts: 72
The following code will print
1: Double a = new Double(Double.NaN);
2: Double b = new Double(Double.NaN);
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");
A) True
B) True
C) False
D) False
The correct answer is C.
Why? A Not-a-Number (NaN) value of type double. It is equal to the value returned by Double.longBitsToDouble(0x7ff8000000000000L).

Thank you

Lydia<br />~~~~~~~~~~~~<br />I love Italy team.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Welcome to Javaranch, a friendly place for Java greenhorns
To answer your question, JLS 4.2.3 Floating-Point Types, Formats, and Values

NaN is unordered, so the numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (�15.20.1). The equality operator == returns false if either operand is NaN, and the inequality operator != returns true if either operand is NaN (�15.21.1). In particular, x!=x is true if and only if x is NaN, and (x<y) == !(x>=y) will be false if x or y is NaN.

Moreover, we'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with the rules. Thank you for your cooperation.
We hope you'll stay around.

[Blog] [Blogroll] [My Reviews] My Linked In
lydia westland
Ranch Hand

Joined: Feb 24, 2002
Posts: 72
Thank you very much. But as for the second if, I think Double value must be changed to double for such comparison. then for the two value of double, it should return false. why is the comparision result is true?
thank you. BTW, Ranch is a good place. I've been watching it for a while. and today is the first time I post messge here.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
thank you for changing your displayed name.
as for your question, try to use the JLS and the API, you'd have got your answer in two mouse clicks:
From the API (class Double, method equals):

If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.

Glad you enjoy it around here
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12682
To understand why the equals method returns true, look at the source code for java.lang.Double.equals()
You will see that it actually compares the long bits (not double values) and that comparison does not care about the special treatment of NaN.


Java Resources at
I agree. Here's the link:
subject: Double.NaN ???
Similar Threads
NaN reference
Q'tion from Majji's paper
Double.NaN question
Mock Q
Double.Nan question.plz help