Shailendra Patel Shailendra Patel - 6 months ago 24
Python Question

Python Division Error

The following division operation in Python returns:

>> 123456789012345678/2
>> 6.172839450617284e+16


While it should return:

>>61728394506172839


Can you tell me the reason behind this and how to fix this? Is this the limit of Base 10 in Python? Because I heard that Python is only limited by the computer's memory.

Answer

Try this, if you are in python 3

123456789012345678//2

In python 3, // refers to integer division (no reminder)

In General in python, however, / refers to division. To get floating point behavior in python < 3, do

float(123456789012345678)/2

Although the real question her is why python misses precision with large integer division. And i don't know why. But this solution might help solve your problem