Size of integers (2**32-1)

Gerhard Häring gh_pythonlist at gmx.de
Tue Feb 5 04:49:34 EST 2002


Le 05/02/02 à 10:16, Thomas Guettler écrivit:
> I have two python version installed:
> with the cygwin version "print 2**32-1" works
> but with the zope version I get OverFlowError.

Zope uses Python 2.1, while your Cygwin Python is 2.2, right?

Python 2.2 does automagically change the type to LongType when the
integers get too big. Python 2.1 doesn't do this, yet.

The reason it fails in Python 2.1 is that 2**32 is not in the range of
the IntType (on your 32 bit platform), IOW it's greater than sys.maxint
(sys.maxint == 2**31-1) Note that in Python 2.1, you can't even say
"print 2**31-1" because it would first need to represent 2**31 as an
IntType, which it cannot.

To represent the number you want in Python < 2.2, you need to say
2L**32-1.

> Background: I want to get a random 32-Bit integer like this
> randrange(0, 2*32-1)

Btw. it looks like random.randint/randrange really want ints and won't
accept longs. A workaround could be to use a construction like:

    print random.randrange(0, sys.maxint) * 2L

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 11.2 °C      Wind: 4.6 m/s




More information about the Python-list mailing list