wood burning stoves*
The moose likes Java in General and the fly likes Comparing Floats Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing Floats" Watch "Comparing Floats" New topic
Author

Comparing Floats

L. Riley
Greenhorn

Joined: Mar 20, 2001
Posts: 28
How do you compare Floats to Floats (wrapper classes)? Some books I've read say always use .equal'. One says that in comparing Floats, "= =" is the same as '.equal'.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Easy enough to test:

Do they both give the same answer? If not, which one gives teh result you want when comparing two Floats with the same wrapped value?


"I'm not back." - Bill Harding, Twister
Zakaria Haque
Ranch Hand

Joined: Jan 02, 2002
Posts: 60
Originally posted by liz riley:
How do you compare Floats to Floats (wrapper classes)? Some books I've read say always use .equal'. One says that in comparing Floats, "= =" is the same as '.equal'.

According to javadoc api spec
"Note that in most cases, for two instances of class Float, f1 and f2, the value of f1.equal.(f2) is true if and only if f1.floatValue() == f2.floatValue()"


tobe bondhu nouka bherao<br />shonabo gaan aj shara raat
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
When comparing Objects you want to use the .equals(..) method. Because when you do == that is checking to see if the references are the same not wether they are storing the same values.
Hope this helps.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
When comparing Objects you want to use the .equals(..) method. Because when you do == that is checking to see if the references are the same not wether they are storing the same values.


If you are referring to f1==f2 that is certainly correct, but the Float wrapper class has a method called floatValue() which returns the corresponding primitive of a Float, in which case, f1.floatValue()==f2.floatValue() is also appropriate for comparing Floats.
-anthony
Thomas Smets
Ranch Hand

Joined: Dec 11, 2001
Posts: 111
Originally posted by Anthony Villanueva:

If you are referring to f1==f2 that is certainly correct, but the Float wrapper class has a method called floatValue() which returns the corresponding primitive of a Float, in which case, f1.floatValue()==f2.floatValue() is also appropriate for comparing Floats.
-anthony


Which is probably what the equals () does !
Thomas,


Thomas Smets
Just another developper
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing Floats
 
Similar Threads
Confused big time
what is the ouptut and how..
Comparing floats/doubles
printf, multiple flags
Wats the O/P...