2

What is the difference between

.3+.3+.3+.1 == 1

which returns false, while

.3+.3+.1+.3 == 1

returns true? This applies to Python as well.

Amro
  • 123,847
  • 25
  • 243
  • 454
schuberm
  • 103
  • 1
  • 7
  • 2
    This has to do with [floating point arithmetic in python](http://docs.python.org/tutorial/floatingpoint.html). – mayhewr May 30 '12 at 21:55
  • 2
    @mayhewr Not in Python. It's floating points arithmetic, period. –  May 30 '12 at 21:59
  • 2
    [Why don’t my numbers add up?](http://floating-point-gui.de), read the [long article too.](http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html) – jfs May 30 '12 at 22:00
  • on the MATLAB side, this has been discussed numerous times. To name a few: [Matlab precision: simple subtraction is not zero](http://stackoverflow.com/q/7918539/97160), [Arithmetic precision with doubles in Matlab](http://stackoverflow.com/q/3697234/97160), [About floating point precision: why the iteration numbers are not equal?](http://stackoverflow.com/q/6477178/97160), [Why is 24.0000 not equal to 24.0000 in MATLAB?](http://stackoverflow.com/q/686439/97160)... – Amro Jun 01 '12 at 01:46

2 Answers2

6

This is due to floating point arithmetic. You can use the ieee754 function to see the floating point representation.

>> ieee754(.3+.3+.3+.1)

ans =

0011111111101111111111111111111111111111111111111111111111111111

>> ieee754(.3+.3+.1+.3)

ans =

0011111111110000000000000000000000000000000000000000000000000000
jkt
  • 2,538
  • 3
  • 26
  • 28
  • Why does the order of the addition change the result? Why does 0.3+0.3+0.3 return 0.89999 while 0.3+0.3 return 0.6? – schuberm May 30 '12 at 22:08
  • I assume 0.6 is exactly representable in 64-bit precision, whereas 0.9 is not. – jkt May 30 '12 at 22:12
  • 0.9 returns 0.9. Is that not exact representation? – schuberm May 30 '12 at 22:25
  • 2
    @YBE 0.6 isn't representable as well. Only those numbers are exactly representable which, multiplied with a power of 2, result in an integer. E.g., 0.375 is nicely even in that sense, because, multiplied with 8 (2**3), it is 3. 0.6 is very bad, however, because you don't find a power of 2 which makes it integer. That is because 0.6 is 6/(2*5) = 3/5, and you can multiply as often with 2 as you want, you don't get rid of the 5. – glglgl May 30 '12 at 22:29
  • 1
    @schuberm It only looks like .9. The closest it can be is 0.90000000000000002220446049250313080847263336181640625. It is very close to .9, but not equal. – glglgl May 30 '12 at 22:30
3

This is a general consequence of finite precision arithmetic in general. The set of possible floating point numbers representable at a given precision forms only a subset of the set of all real numbers. As such, only those numbers that are precisely equal to the finite amount of available floating point representations on a computer. As such unless one of your numbers is exactly the same as its finite precision representation, the actual number represented as bytes in memory will actually only be an approximation. You will then get error propagation when performing arithmetic with these numbers. Do some research into numerical analysis for a much fuller and more precise definition of this kind of thing.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101