GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes float equality 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 equality" Watch "float equality" New topic
Author

float equality

Mert Caliskan
Greenhorn

Joined: May 08, 2007
Posts: 10


why is this outputs false?
Joan Horta Tosas
Ranch Hand

Joined: Feb 01, 2007
Posts: 59
Because f1 and f2 are two different objects. You have to use equals(). But note that using equals() would give false as well, due to IEEE 754 floating-point "single format" bit layout, as it says in Float.equals() there are two exceptions on having the same result on comparing two float objects and two float values:
� If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
� If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.

[ November 03, 2007: Message edited by: Joan Horta Tosas ]
Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89
I think in the wrapper class negative zero strictly smaller than positive zero.However, the methods Math.min and Math.max treat negative zero as beign smaller that positive zero.


"Control time instead of letting time control you."
 
GeeCON Prague 2014
 
subject: float equality