0
votes

I reading about floating point and rounding off errors that occur during the floating point arithmetic.

I read lot of articles on IEEE 754-Single precision / Double precision format. I understand that there is sign bit, 8(or) 11 bits of exponent and 23 (or) 52 bits of significand along with implicit leading bit.

I also know that the real numbers whose denominator is not a prime factor of 2 cannot be exactly representable For E.g 0.1 in binary is 0.0001100110011.....

i understood that 0.1+0.1+0.1 is not equal to 0.3 because the accumulation of rounding error.

Also 0.5 is exactly representable in binary format because it is 1/2. But i don't understand given the above accumulation of rounding error , why 0.1+0.1+0.1+0.1+0.1 = 0.5 ?

2
Are you saying you don't get a rounding error when accumulating 0.1 five times?MooseBoys
i am confused whether the accumulation of error is forsaken when 0.1 is added 5 times or any other arithmetic that leads to a exactly representable number in binary ,if so why?chebus
Only if the new value results in the imprecision being lost in the inaccuracy.Ignacio Vazquez-Abrams
Presumably you determined this by writing some code -- if you show the code we can help you better.Rick Regan
Rick, it's in java boolean b= 0.1+0.1+0.1+0.1+0.1== 0.5//true b=0.1+0.1+0.1==0.3//false my quest why is the reason given for false is not applicable for true case as well, which is round off errors accumulated while arithmetic on Not exactly represent numberschebus

2 Answers

2
votes

In IEEE754 round to nearest even modes you have some nice properties.
First, for any finite float x and n<54, (2^n-1)x+x == 2^nx See Is 3*x+x always exact?

Then you also have (2^n+1)x == 2^nx + x
(well as long as 2^n+1 is exactly representable, n<53).

With these properties, you have

  • 0.1+0.1==2*0.1
  • 0.1+0.1+0.1 == 3*0.1
  • 0.1+0.1+0.1+0.1 == 4*0.1
  • 0.1+0.1+0.1+0.1+0.1 == 5*0.1

This is not enough, because at this stage, 0.1 is not exactly 1/10, so nothing proves that 5*0.1 == 0.5.
For example 3*0.1 != 0.3, and 5*0.3 != 0.15.

So here, it's just luck, the round off error did annihilate instead of cumulate.
(n*0.1 == n/10.0) is true for 65 out of 100 for the integers n from 1 to 100 (allways true for the 7 powers of two in this interval).

1
votes

0.1 in double precision is 0.0001100110011001100110011001100110011001100110011001101 in binary. Let's step through the binary additions to see what's happening:

  0.0001100110011001100110011001100110011001100110011001101
+
  0.0001100110011001100110011001100110011001100110011001101
-----------------------------------------------------------
  0.001100110011001100110011001100110011001100110011001101   (52 sig bits -- OK)
+
  0.0001100110011001100110011001100110011001100110011001101
-----------------------------------------------------------
  0.0100110011001100110011001100110011001100110011001100111  (54 sig bits -- must round to 53)
  0.0100110011001100110011001100110011001100110011001101     (rounded up)
+
  0.0001100110011001100110011001100110011001100110011001101
-----------------------------------------------------------
  0.0110011001100110011001100110011001100110011001100110101  (54 sig bits -- must round to 53)
  0.01100110011001100110011001100110011001100110011001101    (rounded down)
+
  0.0001100110011001100110011001100110011001100110011001101
-----------------------------------------------------------
  0.1000000000000000000000000000000000000000000000000000001 (55 sig bits -- must round to 53)
  0.1                                                       (rounded down)

So just due to how the roundings accumulated, 0.1 added five times became 0.5.

(I got these values from my binary converter, binary calculator, and floating-point converter.)