Integer Overflow

Hans Nowak wurmy at earthlink.net
Tue Nov 27 12:41:38 EST 2001


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).

HTH,

--Hans



More information about the Python-list mailing list