using Double.compare()

P

Prime

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
 
P

Patricia Shanahan

Prime said:
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
 
D

Daniel Pitts

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

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.
 
O

Oliver Wong

[Didn't see the original post, so replying to Daniel's post]


From the Javadocs:
http://java.sun.com/javase/6/docs/api/java/lang/Double.html#compare(double, double)
<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
 
R

Roedy Green

(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.
 
P

Patricia Shanahan

Roedy said:
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
 
T

trstag75

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).

:)
 
O

Oliver Wong

Patricia Shanahan said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top