| Author |
equals method behavior of Float
|
reji singh
Ranch Hand
Joined: Apr 06, 2009
Posts: 52
|
|
Hi All,
As per JDK docs
If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, //1
even though 0.0f==-0.0f has the value true. //2
Doubt: I agree with statement 1 as -0.0f is not equal to 0.0f but why statement 2 is true? It should not be true as per equality of Objects and primitives.
Thanks
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Doubt: I agree with statement 1 as -0.0f is not equal to 0.0f but why statement 2 is true? It should not be true as per equality of Objects and primitives.
Statement 2 is true because that is how it is defined by the IEEE spec. In a comparison, negative zero is equal to positive zero.
As for why statement 1 is false, meaning why it violates IEEE specs -- this was done so maps and sets don't consider positive and negative zero as duplicates of each other.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Tom Kowalski
Ranch Hand
Joined: Feb 17, 2009
Posts: 72
|
|
And don't forget that result depends on whether the variables are Float or float.
|
SCJP, SCWCD, OCUP
|
 |
reji singh
Ranch Hand
Joined: Apr 06, 2009
Posts: 52
|
|
|
Thanks Henry and Tomasz
|
 |
 |
|
|
subject: equals method behavior of Float
|
|
|