pythondivision

Dividing large number by 10 in python gives the wrong result


Say I want to remove the last digit of a number n. For that I use the code int(n/10).

Sadly this gives the wrong result for large numbers. For example n = 4474630975855204960 divided by 10 gives 447463097585520512.

What's the reason for this behavior? How can I fix it?


Solution

  • For some math operations, the Python interpreter will handle long integers for you and you don't have to think about it.

    Division is different and converts integers to floats, which aren't handled well in Python.

    You can get around this by directly using integer division - two // rather than just one /.

    Input

    4474630975855204960//10
    

    Output

    447463097585520496