The smallest and largest values of numeric types

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Wed Apr 18 04:09:34 EDT 2007


On Wed, 18 Apr 2007 07:15:11 +0200, Hendrik van Rooyen wrote:

> I once made a thing that tried to find the limit of longs and stopped
> when I had two or three screenfulls of numbers.

You should find that converting longs to strings (say, for printing them)
is *very* slow. E.g. the following code

big = 10L**100 # one google, a big number
while True:
    print big # so we can see the last value before it fails
    big *= 10

will run terribly slow and should be written as:

big = 10L**100 # one google, a big number
try:
    while True:
        big *= 10
except: # don't know what exception will be raised, so catch ANYTHING
    print len(str(big))-1 # the number of digits

only does the slow conversion to string once, instead of every time around
the loop. However, once your machine starts paging, it will still slow
down a lot.



 
> I came to the conclusion that for "integer" arithmetic like this, the
> limit is either your memory size, or some other number that is so big
> that in practice you don't have to worry about it..

Yes, longs are limited only by the amount of memory accessible. 


-- 
Steven D'Aprano 




More information about the Python-list mailing list