1

I have some strange result in my python 3.6.3. Once i tried some code and i reach this problem.

>>> a = 10**32
>>> print(a/1000/1000)
9.999999999999999e+25

As you see it not actually right, but if i go other way, i reach what i expect

>>> print(a/1000000)
1e+26

Same thing with

>>> 10**26
>>> 10**31

Can somebody explain me what's wrong? i tried write it in one line no result

vaultah
  • 44,105
  • 12
  • 114
  • 143
B31aim
  • 11
  • 4

1 Answers1

1
>>> a = 10**32
>>> a/1000/1000
9.999999999999999e+25

As you know, Python 3 division is no longer an integer division (a//1000//1000 would have worked fine), so you're performing 2 floating point divisions here, introducing an (unnecessary) floating point accumulation error.

>>> a/1000000
1e+26

this only performs one division, so lower floating point error effect, even if result is now floating point.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219