File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Float.NaN Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Float.NaN" Watch "Float.NaN" New topic
Author

Float.NaN

venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
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???
Sathishkumar Ethiraju
Greenhorn

Joined: Nov 23, 2006
Posts: 10
"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

Joined: Apr 29, 2006
Posts: 106
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.
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
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


SCJP 5.0 77%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40



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

Joined: Apr 29, 2006
Posts: 106
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
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

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

Joined: Apr 29, 2006
Posts: 106
Thanks henry for your reply .Even in APIs the havent mentioned anything else.Thanks for helping me.
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Whatever you asked, I had already read in API documentation & Java Language Specification. Try taking a look at them again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Float.NaN