Integer Overflow
Bjorn Pettersen
BPettersen at NAREX.com
Tue Nov 27 12:49:44 EST 2001
> From: Hans Nowak [mailto:wurmy at earthlink.net]
>
> Ursus Horibilis wrote:
> >
> > Is there a way to force the Python run time system to
> ignore integer
> > overflows? I was trying to write a 32-bit Linear
> Congruential Pseudo
> > Random Number Generator
>
> Say what?
>
> > and got clobbered
> > the first time an integer product or sum went over the 32-bit limit.
>
> Use longs:
>
> >>> 2**100
> Traceback (most recent call last):
> File "<pyshell#0>", line 1, in ?
> 2**100
> OverflowError: integer exponentiation
> >>> 2L**100
> 1267650600228229401496703205376L
> >>>
>
> A long integer in Python is simply created by appending an
> "L" to the number, e.g. 2 (normal integer) vs. 2L (long).
And, if you want the result to be in the 32 bit ring, make sure you take
the answer modulo 2L**32...
-- bjorn
More information about the Python-list
mailing list