using Double.compare()

Discussion in 'Java' started by Prime, Mar 13, 2007.

  1. Prime

    Prime Guest

    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
    Prime, Mar 13, 2007
    #1
    1. Advertising

  2. Prime wrote:
    > 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
    Patricia Shanahan, Mar 13, 2007
    #2
    1. Advertising

  3. Prime

    Daniel Pitts Guest

    On Mar 13, 10:57 am, "Prime" <> wrote:
    > 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.
    Daniel Pitts, Mar 13, 2007
    #3
  4. Prime

    Oliver Wong Guest

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

    "Daniel Pitts" <> wrote in message
    news:...
    > On Mar 13, 10:57 am, "Prime" <> wrote:
    >> 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, 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
    Oliver Wong, Mar 13, 2007
    #4
  5. Prime

    Roedy Green Guest

    On 13 Mar 2007 10:57:59 -0700, "Prime" <> wrote,
    quoted or indirectly quoted someone who said :

    >(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.
    --
    Canadian Mind Products, Roedy Green, http://mindprod.com
    Priorities: Prevent global climate destabilisation. End both wars. Prepare for oil shortages.
    Roedy Green, Mar 13, 2007
    #5
  6. Roedy Green wrote:
    > On 13 Mar 2007 10:57:59 -0700, "Prime" <> wrote,
    > quoted or indirectly quoted someone who said :
    >
    >> (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
    Patricia Shanahan, Mar 13, 2007
    #6
  7. Prime

    Guest

    On Mar 13, 5:57 pm, "Prime" <> wrote:
    > 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).

    :)
    , Mar 13, 2007
    #7
  8. Prime

    Oliver Wong Guest

    "Patricia Shanahan" <> wrote in message
    news:et6rk0$2ubd$...
    > Roedy Green wrote:
    >> On 13 Mar 2007 10:57:59 -0700, "Prime" <> wrote,
    >> quoted or indirectly quoted someone who said :
    >>
    >>> (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.


    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
    Oliver Wong, Mar 15, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaja
    Replies:
    0
    Views:
    590
  2. Web learner

    from List <double> to double[]

    Web learner, Apr 25, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    466
  3. sb
    Replies:
    4
    Views:
    297
    Alberto Barbati
    Feb 19, 2004
  4. Jacek Dziedzic
    Replies:
    5
    Views:
    373
    Old Wolf
    Apr 8, 2004
  5. Sydex
    Replies:
    12
    Views:
    6,454
    Victor Bazarov
    Feb 17, 2005
Loading...

Share This Page