0

This is a very basic sum of 5 floats. It's very obvious that the result is 1, so why does python shows me 0.9999999 when i print the result?

sum([.25,.35,.3,.1,0]) # gives 0.999999(9)
azro
  • 53,056
  • 7
  • 34
  • 70
Ricardo Vilaça
  • 846
  • 1
  • 7
  • 18
  • 1
    From the 5 numbers you want to add, only 2 are exactly representable as binary64 floating point numbers. Hence the sum cannot be 1 unless the rounding errors compensate. Generally, when a floating point operation returns a perfect number, it is most likely pure luck unless the numbers are designed to do so. – kvantour Jul 02 '20 at 21:01
  • 1
    Check http://en.wikipedia.org/wiki/Floating-point – Shivam Jha Jul 02 '20 at 21:09

0 Answers0