• 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

Confusion about Double.NaN

 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source: http://www.geocities.com/skmajji/Main.html

Question



When I run the code I get Output




Now my question is that why is giving false.

When i saw the source code i found



SO if they are static and final then why they are not equal?
[ November 28, 2008: Message edited by: Himanshu Gupta ]
 
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
well no NaN values are ==. So NaN == Nan will always be false.

Also I don't think this is a part of SCJP 5 or later but I am not sure...
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I think is that this line public static final double NaN = 0.0d / 0.0; will give only one value; whatever it may be, so it has to be equal. Because there is only one copy of that and that too final.
 
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the thing is when you are doing this

so "Double.NaN" returns some constant value which is actually a long type of value but not a number. But the thing is in a it returns some value which is assigned to it, but in next it again returns the same value but it is considered as from different address or reference. when you are comparing it using "==" it gives false, but while comparing it using equals() it gives true as this method checks the contents, rather than the reference.
 
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

Originally posted by Himanshu Gupta:
What I think is that this line public static final double NaN = 0.0d / 0.0; will give only one value; whatever it may be, so it has to be equal. Because there is only one copy of that and that too final.



Weird right? A floating point value that doesn't equal itself?

This is how it is defined. IEEE defines a floating point value of NAN (not a number) to not equal to itself. This is not specific to Java -- it is true with all languages that support IEEE (which is supported by practically every language, and by every processor).

Now, here's the other weird part. If IEEE defines NAN to not equal itself, why does the Double class equals() method say they do? Doesn't this violate the IEEE standard?

Yes, it does. And the JavaDoc states it as so. The reason the developers chose to break with IEEE in this regard, is because the equals() (and hashCode) method are used in maps. If the equals() method had followed IEEE, it would make it impossible to have a single NAN in a map -- so, they chose to not follow IEEE and document it as such.

Henry
[ November 28, 2008: Message edited by: Henry Wong ]
 
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
Henry can you please confirm if this is in SCJP5 or 6 objectives or not...
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by PUNEET MITTAL:
the thing is when you are doing this

so "Double.NaN" returns some constant value which is actually a long type of value but not a number. But the thing is in a it returns some value which is assigned to it, but in next it again returns the same value but it is considered as from different address or reference. when you are comparing it using "==" it gives false, but while comparing it using equals() it gives true as this method checks the contents, rather than the reference.




class Long extends Number So Long is a number. Moreover I am not asking about some reference value. Here the things I am asking about are static and final. So their values should be same.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.

Your reply made the things very clear. This is the only place where we have such type of aberrance or there are some more ??
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi thanks henry for this wonderful reply. but i have a confusion

If we check the java api for this constant Double.NaN, it says :


public static final double NaN

A constant holding a Not-a-Number (NaN) value of type double. It is equivalent to the value returned by
Double.longBitsToDouble(0x7ff8000000000000L).



but since its not a number. so how can it return a value similar to the above��. That�s wat confusing me.
[ November 28, 2008: Message edited by: PUNEET MITTAL ]
 
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

but since its not a number. so how can it return a value similar to the above��. That�s wat confusing me.



Under the covers, it's all bits and bytes right? ones and zeros? You have to represent "not a number" somehow.

The longBitsToDouble() method, assigns the bits represented by a long as a double. No coversion is done. The bit pattern of the long is simply used as the bit pattern of the double. And that bit pattern is the one that IEEE assigned to NAN.

Henry
[ November 28, 2008: Message edited by: Henry Wong ]
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so you mean to say is that its a binary stream which is in long type and is converted to double type and that double type of bitstream is assigned to NaN, which is returned by Double.NaN.

ok i got it.

thanks henry.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by PUNEET MITTAL:
ok so you mean to say is that its a binary stream which is in long type and is converted to double type and that double type of bitstream is assigned to NaN, which is returned by Double.NaN.

ok i got it.

thanks henry.



Read Henry Post. It says NO conversion is DONE

Henry: The longBitsToDouble() method, assigns the bits represented by a long as a double. No coversion is done. The bit pattern of the long is simply used as the bit pattern of the double. And that bit pattern is the one that IEEE assigned to NAN.

 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
originally posted by ankit:


well no NaN values are ==. So NaN == Nan will always be false.



well ankit, i think you are somewhat misconcepted at this point as NaN is a static final, which means it is a constant term, so the value will always remains same. but the reason for this is different which i think henry has replied for it perfectly.
[ November 28, 2008: Message edited by: PUNEET MITTAL ]
 
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

Originally posted by PUNEET MITTAL:
originally posted by ankit:


well ankit, i think you are somewhat misconcepted at this point as NaN is a static final, which means it is a contant term, so the value will always remains same. but the reason for this is different which i think henry has replied for it perfectly.



Sorry! But actually I think that this is not a part of SCJP so I don't know about it in detail...
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ankit, actually here again i want to tell you that i got the same query earlier in a mock test for scjp5.0, but i forgot to raise this on javaranch, but today i saw it again.

thanks to himanshu for posting such a good question.
[ November 28, 2008: Message edited by: PUNEET MITTAL ]
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I dont know that this comes under objectives of SCJP 1.5 but I have given the source of the mock test website where i found it.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit's suspicion is correct - this kind of info was on the 1.4 and earlier exams, but it's not on the version 5 or 6 exams.

hth,

Bert
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic