Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Float.NaN

venkatesh pendharkar
Ranch Hand
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
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
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
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

Henry Wong
author
Marshal
Posts: 21746
85
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
Marshal
Posts: 21746
85

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
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
Marshal
Posts: 21746
85
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
Thanks henry for your reply .Even in APIs the havent mentioned anything else.Thanks for helping me.

Abdul Rehman
Ranch Hand
Posts: 65
Whatever you asked, I had already read in API documentation & Java Language Specification. Try taking a look at them again