Message365830
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. |
|
Date |
User |
Action |
Args |
2020-04-05 23:13:05 | remi.lapeyre | set | recipients:
+ remi.lapeyre, talha.demirsoy |
2020-04-05 23:13:04 | remi.lapeyre | set | messageid: <1586128384.99.0.205573172775.issue40200@roundup.psfhosted.org> |
2020-04-05 23:13:04 | remi.lapeyre | link | issue40200 messages |
2020-04-05 23:13:04 | remi.lapeyre | create | |
|