0

Python Documents says,

For floating point numbers, this truncates towards zero. https://docs.python.org/3/library/functions.html?highlight=int#int

But when I tried assign a large float number to an int () function, it doesn't work well.

>>> int(11111111111111111/2)
5555555555555556
>>> 11111111111111111//2
5555555555555555

What happen?? (rounding error??)

  • 1
    Hint: what happens when you try `11111111111111111/2`, *without* the `int`? What happens if you try `float(11111111111111111)`? – Karl Knechtel Dec 19 '21 at 18:42
  • `5555555555555555.5` has too many digits of precision for floating point, it can't be represented exactly. – Barmar Dec 19 '21 at 18:42
  • See also https://stackoverflow.com/questions/3793838/which-is-the-first-integer-that-an-ieee-754-float-is-incapable-of-representing-e – Karl Knechtel Dec 19 '21 at 18:43
  • >>> float(1111111111111111111) 1.1111111111111112e+18 This is very simple and essential answer ! Thank you so much!! – gen.komatsu Dec 21 '21 at 10:53

0 Answers0