• 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

Float.NaN

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me why :-
"System.out.println(Float.NaN==Float.NaN);" returns false &

Float f1=new Float(Float.NaN);
Float f2=new Float(Float.NaN);
System.out.println(f1.equals(f2)); prints true..

Whats the differnece beewwen the two???
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"System.out.println(Float.NaN==Float.NaN);" returns false
------ Coz in abv statement two different reference were compared
Float f1=new Float(Float.NaN);
Float f2=new Float(Float.NaN);
System.out.println(f1.equals(f2)); prints true..
------ here true, coz equals method will be overrided to compare the actual values, not the references. Am i right ?
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I think Float.NaN doesnt represent a reference,it is a primitive float value ,we can assign this value to a primitive variable such as
float f=Float.NaN;

So if it is a primitive value then why
System.out.println(Float.NaN==Float.NaN); prints false

i understand why System.out.println(f1.equals(f2)); prints true as equals() method is overridden in Float class to compare its contents according to valueOf() method.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats really surprising. I just saw the source code of Float class. And this is how NaN was defined:

public static final float NaN = 0.0f / 0.0f;



Going by this Float.Nan == Float.NaN must yield result as true. But i get false. Gurus please throw some light into this
 
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 Satish Kota:

Going by this Float.Nan == Float.NaN must yield result as true. But i get false. Gurus please throw some light into this



NaN (or Not A Number) is defined by IEEE to be returned with certain floating point operations. IEEE also defines how it is to be compared -- it is not equal to any number, including another NaN.

Henry
 
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


The Float.equals() method converts the floating point value to int bits, which is used to compare. This doesn't follow IEEE specifications. I believe this was done to allow hashing to work correctly (according to the JavaDoc).

Henry
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Henry ,thanks for explaining this.
there is one more thing i would like to know....
Whats the difference between +0.0 & -0.0.Why in float we have positive negative value for 0.
I have observed that Math.ceil(-0.2) gives -0.0 ... but Math.floor(0.2) gives 0.0.

Also i dont understand why
Float f1=new Float(0.0);
Float f2=new Float(-0.0);
System.out.println(f1.equals(f2));
prints false but
System.out.println(0.0==-0.0); prints true.
 
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
I don't know enough of the IEEE standard to tell you want generates a negative zero versus what generates a positive zero. Or is what situations (of usage) where they generated different results... but....


The reason positive zero is equal to negative zero, is because the IEEE specifications say so. And the reason they are not equal using the Float class is because they have different int bit values -- another case where the Float class doesn't follow IEEE spec.

Henry
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks henry for your reply .Even in APIs the havent mentioned anything else.Thanks for helping me.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whatever you asked, I had already read in API documentation & Java Language Specification. Try taking a look at them again
 
Look ma! I'm selling my stuff!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic