Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

using Double.compare()

Thread view: 
Prime - 13 Mar 2007 18:57 GMT
Is there any particular reason(s) why I should use the
Double.compare() method?

(i.e. Double.compare(<double1>, <double2>) == 0, instead of just
saying double1 == double2 ??)

Thanks

Patrick
Patricia Shanahan - 13 Mar 2007 19:03 GMT
> Is there any particular reason(s) why I should use the
> Double.compare() method?
>
> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
> saying double1 == double2 ??)

It's useful for building compareTo (interface Comparable) and compare
(interface Comparator) methods when the comparison criterion is the
value of a double.

Patricia
Daniel Pitts - 13 Mar 2007 19:12 GMT
> Is there any particular reason(s) why I should use the
> Double.compare() method?
[quoted text clipped - 5 lines]
>
> Patrick

Its generally a bad idea to compare floating point numbers for
equality, due to rounding errors, you will find situations where your
comparison fails unexpectedly.
Oliver Wong - 13 Mar 2007 19:32 GMT
[Didn't see the original post, so replying to Daniel's post]

>> Is there any particular reason(s) why I should use the
>> Double.compare() method?
>>
>> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
>> saying double1 == double2 ??)

From the Javadocs:
http://java.sun.com/javase/6/docs/api/java/lang/Double.html#compare(double,%20double)
<quote>
There are two ways in which comparisons performed by this method differ
from those performed by the Java language numerical comparison operators
(<, <=, ==, >= >) when applied to primitive double values:

   * Double.NaN is considered by this method to be equal to itself and
greater than all other double values (including Double.POSITIVE_INFINITY).
   * 0.0d is considered by this method to be greater than -0.0d.
</quote>

   - Oliver
Roedy Green - 13 Mar 2007 19:32 GMT
>(i.e. Double.compare(<double1>, <double2>) == 0, instead of just
>saying double1 == double2 ??)

They do different things.  == ensures you are pointing to the same
object.  Compare ensures two different objects contain the same value.
Signature

Canadian Mind Products, Roedy Green, http://mindprod.com
Priorities: Prevent global climate destabilisation. End both wars. Prepare for oil shortages.

Patricia Shanahan - 13 Mar 2007 19:47 GMT
>> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
>> saying double1 == double2 ??)
>
> They do different things.  == ensures you are pointing to the same
> object.  Compare ensures two different objects contain the same value.

I don't understand this comment. Double.compare takes two arguments of
type double. If double1 and double2 are suitable arguments for
Double.compare then they are primitives, and == compares their values.

Patricia
Oliver Wong - 15 Mar 2007 15:15 GMT
>>> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
>>> saying double1 == double2 ??)
[quoted text clipped - 5 lines]
> type double. If double1 and double2 are suitable arguments for
> Double.compare then they are primitives, and == compares their values.

   double1 and double2 may be instances of Double, and get auto-unboxed
when passed to Double.compare() (which might then say they are equal), and
yet be different objects (and thus == says they are not equal).

   - Oliver
trstag75@yahoo.fr - 13 Mar 2007 21:09 GMT
> Is there any particular reason(s) why I should use the
> Double.compare() method?

I'll answer that but for a start: you probably, as Daniel noted,
do *not* want to directly compare double.  At least, if you ask
the question, you probably do not know all the details involved.

You probably want to read this (and want to learn what an
epsilon, in this context, is):

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Now to answer your question...

> (i.e. Double.compare(<double1>, <double2>) == 0, instead of just
> saying double1 == double2 ??)

What are double1 and double2? Objects? Primitives? What Java version?
Can auto-boxing take place?

       final Double a = new Double( 0.1d );
       final Double b = new Double( 0.1d );
       System.out.println( "a == b ? " + (a == b) );
       System.out.println( "Double.compare(a,b) == 0: " +
(Double.compare(a,b) == 0) );
       final double c = 0.1d;
       final double d = 0.1d;
       System.out.println( "c == d ? " + (c == d) );
       System.out.println( "Double.compare(c,d) == 0: " +
(Double.compare(c,d) == 0) );

Will output:

a == b ? false
Double.compare(a,b) == 0: true
c == d ? true
Double.compare(c,d) == 0: true

When you compare a and b with == you're comparing the
references of the two objects.  As I specifically used
"new" to construct them, the JVM MUST (by the specs)
create two different objects. Hence a == b returns false
(the references are not pointing to the same object).

When you compare c and d with ==, you're comparing
primitives.

Now, a little more fun:

       final double e = 0.1d;
       final double f = 0.3d;
       final double g = 0.2d;
       final double h = f - g;   // we're substracting 0.2 to 0.3 or
are we?
       System.out.println( "h == e ? " + (h == e) );
       System.out.println( "e is " + e );
       System.out.println( "h is " + h );

will output:

h == e ? false
e is 0.1
h is 0.09999999999999998

And the fact Java actually does output "e is 0.1" is really
interesting (hint: just as most decimal numbers, it's
impossible to represent 0.1 using Java's floating point numbers).

:)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.