This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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."
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: float equality
 
Similar Threads
NaN values..
Usage of :- int comapareTo(float f)
equals method behavior of Float
Question on NAN
Understanding NaN