Rémi Lapeyre <remi.lape...@henki.fr> added the comment:

Hi Talha, you are using floating points division which convert its operands to 
floats so it loose precision for large numbers. The syntax for integer division 
in Python 3 is // and it will not change the type of its operands. Notice the 
difference below:

>>> 100000000000000000000000000/10 % 10
4.0
>>> 100000000000000000000000000.0//10 % 10
4.0
>>> 100000000000000000000000000//10 % 10
0

As you can see, in the first example the operand got changed to float which 
caused a loss of precision and we get the same result when we try directly with 
a float. Using // gives the expected result.

Python use perfect arithmetic for integers but IEEE 754 for floating point 
calculations. You will find that there is a lot of those "quirks" when using 
either very large or very small numbers and will need to be mindful of them.

In the program you linked, changing '/' to '//' should gives the result you are 
expecting.

----------
nosy: +remi.lapeyre -furkanonder

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40200>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to