0

I would want know why when I sum 5.9 plus 4.2 in python, it returns 10.100000000000001, i.e., why 0.000000000000001?

Any explain will be well received.

Sofía
  • 1
  • It is not just Python. This is s known computer issue. See above SO forum – Prayson W. Daniel Jun 25 '21 at 03:20
  • This happens because float numbers are stored as binary values and some numbers, when represented in binary values, have infinite digits (just like 1/3 in decimal is 0.3...). But since you cannot have infinite memory to store all the digits, you have to truncate it. This makes small non exact representations of numbers. Different computers deals with it differently, but in essence all computers do the same. – Marcos Vinícius Petri Jun 25 '21 at 03:22
  • https://docs.python.org/3/tutorial/floatingpoint.html info on display of floats and format tips in [tag:python] – etch_45 Jun 25 '21 at 03:23
  • Others have told you to take into account that reals can only be approximated by binary fractions, *I'd like to show you that the approximation is* **really** *good*: the relative error that you have encountered is (10.100000000000001-10.1)/10.1 ⇒ 1.76/10¹⁶, now imagine that, at a certain moment, we measure the distance Earth-Sun with the same relative error... because the mean distance is 150·10⁶ km = 1.5·10¹⁴ mm, an estimate of the absolute error is (1.50*1.76/100) mm, or 0.026 mm. I hope to have convinced you that the error introduced by the binary approximation is usually not relevant. – gboffi Jun 25 '21 at 06:38

0 Answers0