-1

been playing a bit with numerical stability and found this:

>>> sum([1e4,1e20,-1e20])
16384.0

Any ideas why is this happening?

  • 6
    Possible duplicate of [Why Are Floating Point Numbers Inaccurate?](http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Kevin Nov 11 '16 at 23:33
  • Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Eli Sadoff Nov 11 '16 at 23:33

1 Answers1

2

The first two numbers can't be summed accurately because Python's floating point representation doesn't support that many significant decimal digits (it supports 16 digits and your sum requires 17 to be represented accurately). Python is approximating the answer with a single bit in the least significant part of the mantissa.

The difference between the answer you get after adding the third number and the answer you are expecting represents the error in the representation of the intermediate result. After that subtraction, that single bit in the mantissa is all that is left; when the exponent is normalized, you are left with 16384. The fact that it is a power of two tips you off to what is happening.

antlersoft
  • 14,636
  • 4
  • 35
  • 55