thekushalghosh's blog

By thekushalghosh, history, 4 months ago, , This is My First Blog on Codeforces. Most of you, who use Python must be knowing about the floor division operator(//) in Python. For those who don't know, this operator returns the floor value after division. For Example : 5 / 2 = 2.5, but 5 // 2 = 2 (2 is the floor value of 2.5)

But the division operator behaves abnormally generally for numbers greater than 10 ^ 17.

x = 10000000000000000000006
if x / 2 == x // 2:
print("Hello")
else:
print("World")

For the above code, World will be printed and not Hello. This is because 10000000000000000000006 / 2 will return 5e + 21, but 10000000000000000000006 // 2 will return the correct answer 5000000000000000000003. Even int(10000000000000000000006 / 2) will return 5000000000000000000000, which is incorrect.

Therefore even if you want to divide large numbers, use // operator.

For Example : If you want to find sum of first 100000000000000000000000010002 numbers, with formula : n(n + 1)/2, the normal division operator(/) will give you incorrect answer, but // operator will give you correct answer.

PS : If you would like to downvote, please go ahead, but please tell me what's wrong in the comments  Comments (1)
 » Great insight!