The answer should be 163271004971660485
, but it gives 163271004971660480
as the answer. Using int(326542009943320970/2)
gives the same incorrect result.
Asked
Active
Viewed 31 times
0
-
Did you try `round(326542009943320970//2)` ? – Austin Jun 05 '20 at 08:35
-
I'm not so versed in Python, but I found this link to be useful: https://realpython.com/python-rounding/ – Paul Jun 05 '20 at 08:37
-
See also [Python and “arbitrary precision integers”](https://stackoverflow.com/questions/21031093/python-and-arbitrary-precision-integers) – Pac0 Jun 05 '20 at 08:38
-
Short version of the duplicate: your number is too large to represent as a float, and `/` is floating-point division. You need to start with integer division `//` – `326542009943320970 // 2` is the correct, precise version of the incorrect `int(326542009943320970 / 2)` – then apply rounding manually by adding 1 based on whether the input is even or odd with the help of the `%` operator. – Ry- Jun 05 '20 at 08:39
-
@Austin no need for`round`, but `//` is indeed important here – Pac0 Jun 05 '20 at 08:39
-
@Austin Thanks, that works. Do you know what causes this problem? – noob Jun 05 '20 at 08:39
-
@noob seems Ry was writing the explanation while you were writing your comment :) – Pac0 Jun 05 '20 at 08:40
-
Please see the linked duplicate. – Austin Jun 05 '20 at 08:41