• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

NAN Question,Please help.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when i ran code, got False,Ture;don't know why,anyone can explain it?TIA
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question really has nothing to do with NaN. It's about object equivalence.
Two different objects (like your Double objects) can NEVER be equivalent, ie, the same object, because they are different objects.
Object ob1 = new Object();
Object ob2 = new Object();
ob1 and ob2 are two different Objects, so this code will always evaluate to false:
ob1 == ob2.
This is the same situation you have with comparing your doubles. You have

'a' and 'b' are two different objects, so they can NEVER return true when you write
a == b.
[ April 17, 2002: Message edited by: Rob Ross ]
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Will...
a and b have the same value but do not reference the same object. thus, that is why you see the results you see.
 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.
Thank you. Rob and Ricardo.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer to the JLS �4.2.3 and �4.2.4 to see why
Double.NaN == Double.NaN
is false.
Also, see the JavaDocs for java.lang.Double to see why
a.equals(b)
is true.
Junilu
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe Im missing something.. but where does the code have a==b?
I see Double.NAN.. but that has nothing to do with either object a or b..
I see a.equals(b).. but again.. no a==b
Just remember that if f1 and f2 are NaN, then f1.equals(f2) returns true, and that Float.NaN==Float.NaN returns false.
This is the opposite of: if f1 is 0.0f and f2 is -0.0f, then f1.equals(f2) is false and that 0.0f==-0.0f is true.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joshua Kueck:
Maybe Im missing something.. but where does the code have a==b?


Inside the code tags of Rob's example in his reply.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joshua is right, my bad. I must have been half-asleep. I saw the Double, and the ==, and thought he was trying to compare two objects , but in reality he's comparing two floating point literals that are NaN.
Sorry.
Move along.
Nothing to see here
:roll:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic