You divide by using the true / operator, which will always have floating point values. Instead, use // to divide the sex to get integer results:
>>> a = 313585730727764141482831584863 >>> b = a*2 >>> c = b
Computer hardware cannot process floating point values โโwith the required accuracy.
An alternative is to use decimal.Decimal() values , but this will lead to slower arithmetic operations.
In Python 2, the / operator is a gender division operator, but applies only to integers. To get the same behavior in Python 2, add:
from __future__ import division
The behavior was changed because the difference between using only integer operators and using at least one floating-point argument was confusing.
In other words, the standard Python 2 / operator is another beast from the Python 3 / split operator. When applied to two integer operands, it acts the same as the // floor split operator // in Python 3. But if one of the two operands is a float, then it acts like the / float division operator. The above __future__ import collapses the Python 2 / operator for the actual division operator found in Python 3.
This can be seen when parsing Python bytecode:
>>> import dis >>> def olddivision(x, y): return x / y ... >>> dis.dis(olddivision) 1 0 LOAD_FAST 0 (x) 3 LOAD_FAST 1 (y) 6 BINARY_DIVIDE 7 RETURN_VALUE >>> from __future__ import division >>> def newdivision(x, y): return x / y ... >>> dis.dis(newdivision) 1 0 LOAD_FAST 0 (x) 3 LOAD_FAST 1 (y) 6 BINARY_TRUE_DIVIDE 7 RETURN_VALUE
importing __future__ forced the Python compiler to use a different bytecode for the division operator, replacing BINARY_DIVIDE with BINARY_TRUE_DIVIDE .
Martijn pieters
source share