[Tutor] puzzled by Python 3's print()
Eike Welk
eike.welk at gmx.net
Thu Jul 1 21:18:00 CEST 2010
Hello Richard!
On Thursday July 1 2010 15:11:21 Richard D. Moores wrote:
> Thanks to yours and others responses, I've learned some things I
> didn't know, but remember, I'm starting with long ints such as
Also note that in Python 3 the "/" (division) operator returns a floating
point number when you divide integers. This is one of the changes that Python
3 introduces.
As you are using long integers (and you were previously writing about prime
numbers) the precision of floating point numbers might not be enough for your
purposes.
Therefore you should probably use the integer division operator: "//"
The following (edited) snippet from IPython demonstrates "//" and the loss of
precision when using "/":
...
Python 2.6.2 (r262:71600, Mar 29 2010, 15:30:01)
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
...
In [1]: from __future__ import division
In [2]: a = 1000000000000000000000000000000000000000000000000000000002
In [3]: a
Out[3]: 1000000000000000000000000000000000000000000000000000000002L
In [4]: a//2
Out[4]: 500000000000000000000000000000000000000000000000000000001L
In [5]: a/2
Out[5]: 4.9999999999999994e+56
In [6]: long(a/2)
Out[6]: 499999999999999937061060126016582882140297920412594995200L
Eike.
More information about the Tutor
mailing list