• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NaN in Double class

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..




 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pats read the statement again. Its (x<y) == !(x>=y) and not (x<y) == (x>=y)
 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Interesting topic, but NOT on the exam.

hth,

Bert
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic