performance problem in python 2.2

Jeff Davis jdavis at empires.org
Fri Jul 26 20:40:34 EDT 2002


> 
> No, on most computers a C integer can't represent numbers like 2**64.
> It only has 32 bits.  If you multiply numbers together that overflow
> 32 bits, you'll get the wrong answer unless the language mysteriously
> converted them to floats for you (the rules for that are slightly
> obscure).
> 

If you read my C code, you'll see that I used the 'unsigned long long type' 
which is a 64-bit int. And I only stored 2^32 in it. When I needed to 
crunch the numbers, I treated 2^64 as p*p (because 2^64 == 
sqrt(2^64)*sqrt(2^64) == 2^32*2^32), and then I commuted the 
multiplication so it would happen in a way that wouldn't overflow. Maybe 
it isn't a solid algorithm, but it appears to have mostly worked, and as 
you can see I was thinking about something while I designed it. Granted, 
it was more a "sounded good at the time" kind of thing, but I at least 
don't think I'm overflowing my ints.

Regards,
        Jeff



More information about the Python-list mailing list