Results 1 to 5 of 5
  1. #1
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Should you always use Double.compare( double d1, double d2 ) for >= and <=?

    Hello all.

    I was looking over the API and reading some documentation. There is a compelling case to when trying to do a direct comparison ( == ) for one double to be equal to another double.

    I was wanting to know if this is really necessary when using <, <=, >, or >= operators?

    Thanks.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Should you always use Double.compare( double d1, double d2 ) for >= and <=?

    Primitve types (such a double with a lower case 'd') can (and must) be compared with the operators <, <=, >, >=, == and !=. It's only the class types (such as Double etc.) that need to be compared using a method such as the compareTo( ... ) method.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Should you always use Double.compare( double d1, double d2 ) for >= and <=?

    Quote Originally Posted by JosAH View Post
    Primitve types (such a double with a lower case 'd') can (and must) be compared with the operators <, <=, >, >=, == and !=. It's only the class types (such as Double etc.) that need to be compared using a method such as the compareTo( ... ) method.

    kind regards,

    Jos
    The Double.compare( double d1, double d2 ) take primitive arguments?
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,937
    Rep Power
    20

    Default Re: Should you always use Double.compare( double d1, double d2 ) for >= and <=?

    compare(...) is not compareTo(...)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Should you always use Double.compare( double d1, double d2 ) for >= and <=?

    Quote Originally Posted by stchman View Post
    The Double.compare( double d1, double d2 ) take primitive arguments?
    Carefully read the API documentation for the Double class.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  2. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 02:51 PM
  3. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  4. Double
    By Xystus777 in forum New To Java
    Replies: 2
    Last Post: 01-21-2009, 11:39 AM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •