why error double + double ???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nguyen Chau Son
    New Member
    • Jul 2011
    • 4

    why error double + double ???

    Code:

    double total = 0;
    double a1 = 28.69;
    double a2 = 14.21;
    total = a1 + a2;

    I builde it in Vs2008,and get the result as:
    total = 42.900000000000 006
    Attached Files
  • William Di Luig
    New Member
    • Jul 2011
    • 3

    #2
    It's because of the precision. In fact, when you do operations with doubles you can't expect to have perfect results.

    It is often useful to compare doubles using a particular technique, instead of writing:

    Code:
    double a, b;
    if (a == b)
    you use an "eps" constant (very very little) and do the inverse:

    Code:
    const double eps = 1e-9;
    double a, b;
    if ( fabs(a-b) < eps)

    Comment

    • Nguyen Chau Son
      New Member
      • Jul 2011
      • 4

      #3
      But when I use the two operators is the decimal it for the right result. The comparison with how you say it will only be used when applied to some cases but if so when you have to compare like to see an addition is correct?

      Comment

      • William Di Luig
        New Member
        • Jul 2011
        • 3

        #4
        I dont' understand you.

        If you have to check the correctness of an addition you can use EPS.

        Code:
        if (total == a1+a2)
        becomes
        Code:
        if ( fabs(total-(a1+a2)) < EPS)

        Comment

        • Nguyen Chau Son
          New Member
          • Jul 2011
          • 4

          #5
          my mind to ask why there is no exact results when added two double numbers, but exactly when add two decimal numbers.

          You have a comprehensive solution for the case added two double numbers this?

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            The problems you are encountering are part of basic computer science.
            That is how a computer stores floating point numbers.
            I recommend some background reading on this subject

            When comparing floats it is general practice to set a tolerance, which is what Willian explained to you.

            You define a constant EPS (epsilon) as the acceptable tolerance say
            0.0000001.

            Then subtract actual and expected results and compare to EPS as William showed.
            if outside the tolerance the result is rejected.

            Comment

            • William Di Luig
              New Member
              • Jul 2011
              • 3

              #7
              Look here to understand how double works.

              Comment

              • Nguyen Chau Son
                New Member
                • Jul 2011
                • 4

                #8
                If the comparison as you say, when I perform operations that are intermediate calculations over to do it the way you say?

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  Interesting link William.
                  I'm afraid I could not understand the OP last question

                  Comment

                  • bvrwoo
                    New Member
                    • Aug 2011
                    • 16

                    #10
                    Read on conversion. No percision is exact with double (and even floats). You have to remember that you are using 8 bytes of memory with 64 bits of 1 and 0's in machine code for your number. It can only get so precise with floating points of exponents (include 32bit (float) exponent of 8).

                    Comment

                    Working...