Integer Overflow

Fernando Pérez fperez528 at yahoo.com
Mon Nov 26 06:21:35 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 and got clobbered
> the first time an integer product or sum went over the 32-bit
> limit.

well, you can always put the relevant parts in a try:..except block, but I 
suspect that will kill the speed you need for a rng. If it's just an academic 
algorithm exercise, do it that way. If you actually need performance, why 
don't you look at the random module? That may give you some ideas.

Out of curiosity, though: I've never written a rng myself. In C, an integer 
overflow silently goes to 0. Are you relying on this 'feature' in your 
algorithm? I'm just curious about to whether asking to ignore an overflow 
without triggering an exception is a design feature or a flaw of the way you 
are doing things.

Cheers,

f.



More information about the Python-list mailing list