Results 1 to 11 of 11
  1. #1
    009
    009 is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default how to compare double

    hi how can i compare two double values

    is this correct

    Java Code:
    public boolean isEqualToD(double s_src, double s_compareTo)
        {
    
            boolean flag = false;
            try
            {
               
                if(s_src == s_compareTo)    
                {
                    flag = true;
                }
            } catch (NullPointerException npe)
            {
                logger.log(Level.FATAL, npe.getMessage(), npe);
                return false;
            }
    
            return flag;
        }
    Last edited by 009; 06-17-2014 at 11:55 AM.

  2. #2
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default Re: how to compare double

    Yes You are right.

    In general, if both variables are primitive, better use ==, and if both variables are objects, then better use equals() method for comparison. Keep this in your mind. It will obviously be useful in future
    Mak
    (Living @ Virtual World)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to compare double

    Well, the whole try/catch is completely unnecessary as there's no possible exception that can be thrown from those statements.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: how to compare double

    Quote Originally Posted by Tolls View Post
    Well, the whole try/catch is completely unnecessary as there's no possible exception that can be thrown from those statements.
    Except for the ParanoiaException and YouNeverKnowError
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: how to compare double

    That works! But why write a method? Just do the comparison.

    Java Code:
    if(s_src == s_compareTo) {
       // do something
    } else {
      // do something else
    }
    If you want to retain the comparison state, you can do the following:

    Java Code:
    boolean isEqualToD = s_src == s_compareTo;
    //
    if (isEqualToD) {
       // do something
    } else {
       // do something else
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to compare double

    Theoretically speaking you can compare two doubles for equality with the == operator, but be careful:

    Java Code:
    double a= 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
    		
    System.out.println(a == 1.0);
    This code snippet prints 'false' while mathematically 10*0.1 == 1.0; better test for an absolute error or relative error:

    Java Code:
    double absError= Math.abs(a-b);
    double max= Math.max(Math.abs(a), Math.abs(b));
    double relError= Math.abs((a-b)/max);
    If both a and b are zero, take care not to use the second formula blindly, otherwise, check if relError and/or absError are less than a small value epsilon.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: how to compare double

    Good point. There is a link on this forum to an article about what everyone should know about floating point. I can't find it though. Wouldn't mind reading it again myself.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: how to compare double

    Apache's MathUtils has a compareTo for doubles which allows you to provide an error margin.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to compare double

    Quote Originally Posted by jim829 View Post
    Good point. There is a link on this forum to an article about what everyone should know about floating point. I can't find it though. Wouldn't mind reading it again myself.
    Here it is: What Every Computer Scientist Should Know About Floating-Point Arithmetic

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: how to compare double

    Great! Thanks!

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to compare double

    Quote Originally Posted by jim829 View Post
    Great! Thanks!
    You're welcome; after reading it (again), you'll know everything about ulps and other misery in the floating point world ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 05-10-2012, 08:03 AM
  2. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  3. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 03:51 PM
  4. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  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
  •