Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NaN in Double class

 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all!

I am so confused about the following outputs! In the api code for Double that i saw had the following comment before the declaration of NaN constant:




If this is so, how come the two values in LHS and RHS are unequal?? The type of NaN is a primitive double, not an object reference..




 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this excerpt from the JLS answers your question

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.
 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:I think this excerpt from the JLS answers your question

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.



Hi Ankit,

Thanks for your reply.

But did not quite understand the last part of the paragraph.
They say, (x<y) == !(x>=y) will be false if x or y is NaN.
But this also applies to the following:

Say,

x = 0;
y = 1;

(x<y) -> 0 < 1 -> true
(x>=y) -> 0 >= 1 -> false
hence the lhs and rhs are not equal and hence (x<y) == !(x>=y) is false even though neither of them are NaN

Thanks
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pats read the statement again. Its (x<y) == !(x>=y) and not (x<y) == (x>=y)
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me from where did you get this type of question.is it a SCJP question ??
Please tell me from which book you got this question.


Can you give me an example with regards to positive and negative infinity. ??

--
Deepak Lal
 
Henry Wong
author
Marshal
Pie
Posts: 21389
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is so, how come the two values in LHS and RHS are unequal?? The type of NaN is a primitive double, not an object reference..


The simple answer here is.... The IEEE floating point standard defines it as so -- NaN does not equal NaN. And Java is simply supporting the standard.

As a side note, the equals() method will report it as equal, if they are Double objects. This is in direct violation of the standard, and the JavaDoc states it as so. The reason this is necessary is because it will break the usability of the certain collections if NaN does not equal NaN.

Henry
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,
Could you please provide me an example for Nan when used with Double. ?
Is this a SCJP question,if so which book demonstrated this concept.


Help provided will be highly appreciated
--
Deepak Lal
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Interesting topic, but NOT on the exam.

hth,

Bert
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bert ,
Could you please give us an example atleast on NaN used with Double class.??

--
Deepak Lal

 
Henry Wong
author
Marshal
Pie
Posts: 21389
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Lal wrote:Hi Bert ,
Could you please give us an example atleast on NaN used with Double class.??


No offense here -- but you are a SCJP candidate. It would take less than a minute to create a method, that creates two Double instances (assigned to NaN), and test the equals() method. What is it that is preventing you from writing this simple example?

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic