1

The code below is not giving exact value as expected. What could be the problem in computation.

double a=1.0,b=3.0,c,x;
c=a/b;
printf("c= a/b = %.15lf div=%.15lf\n",c,a/b);
x=c+c+c;
printf("x= c+c+c = %.15lf   Add(c+c+c)=%.15lf\n",x,c+c+c);
x=a/b+a/b+a/b;
printf("x= a/b+a/b+a/b = %.15lf Add(a/b+a/b+a/b)=%.15lf\n",x,a/b+a/b+a/b);

and the result shown up is

c= a/b = 0.333333333333333  div=0.333333333333333
x= c+c+c = 1.000000000000000    Add(c+c+c)=1.000000000000000
x= a/b+a/b+a/b = 1.000000000000000  Add(a/b+a/b+a/b)=1.000000000000000

than the expected value for 'x' which is 0.999999999999999 why?

Rakesh
  • 133
  • 1
  • 8
  • 1
    Assuming `double` is IEEE 754 64-bit binary, `c` is 0.333333333333333314829616256247390992939472198486328125. The exact value of `c+c+c` is 0.999999999999999944488848768742172978818416595458984375. That is exactly half way between two representable numbers, 0.99999999999999988897769753748434595763683319091796875 and 1.0. Round-to-even makes the result 1.0. – Patricia Shanahan May 15 '17 at 08:28
  • Curious, why did you use 15 in `printf("c= a/b = %.15lf ...` and not something like 10 or 20? – chux - Reinstate Monica May 15 '17 at 13:58
  • @chux I just wanted to check which position onwards exactly the digit is changing and is it >4 which might result in 1.00000... i.e., for eg. 0.33334 + 0.33334 + 0.33334 =1.00002 which might be rounded to 1.0000. – Rakesh May 16 '17 at 05:11
  • A `double` can encode about 2^64 different numbers - exactly. One-third is not one of them. What value would you expect `c` to have with `c = 1.0/3.0;`? – chux - Reinstate Monica May 16 '17 at 05:17
  • @Patricia Shanahan I tried this code to see will it round the resulting value `double l=0.3; double k=l+l+l+0.1; printf("0.1 = %.25lf 0.3+0.3+0.3 = %.25lf; k =%.25lf\n",0.1,0.3+0.3+0.3,k);` And the output is: `0.1 = 0.1000000000000000055511151 0.3+0.3+0.3 = 0.8999999999999999111821580; k =0.99999999999999988897769750` Why the 'k' value is not rounded to 1.0 when it is rounded for (1.0/3.0)+(1.0/3.0)+(1.0/3.0)? We can also observe that, though 16th digit in 0.1 representation is 0; 16th digit in the result of (0.3+0.3+0.3) is 9 but in 'k' it is 8. why is this difference? – Rakesh May 16 '17 at 05:37
  • @chux I was under the impression that beyond 16 digits for double number representation would be some random digits, but with Patricia Shanahan's explanation, I was expecting c=0.333333333333333314829616256247390992939472198486328125 and x=0.999999999999999944488848768742172978818416595458984375 but the result was rounded to 1.0...!! :-( – Rakesh May 16 '17 at 05:48
  • @Rakesh If you want to learn more about this, read the answers to the duplicate question. – Patricia Shanahan May 16 '17 at 07:26
  • The number of digits printed has nothing to do with the addition of `0.3+0.3+0.3` . The result of the math is easier to understand when the value is printed in hex/binary notation: `printf("%a\n", 0.3); printf("%a\n", 0.3+0.3+0.3);` --> `0x1.3333333333333p-2 0x1.ccccccccccccdp-1` – chux - Reinstate Monica May 16 '17 at 13:55

0 Answers0