The smallest and largest values of numeric types

mensanator at aol.com mensanator at aol.com
Wed Apr 18 16:35:42 CEST 2007


On Apr 18, 3:09�am, Steven D'Aprano <s... at REMOVEME.cybersource.com.au>
wrote:
> 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.

But there may be other limitations even if you have the memory.

For example, this test is limited to generation 10
because tne 11th generation produces "outrageous
exponent" error. Here, every 9th 1st generation,
starting from the fifth is a second generation, every
9th sencond, starting from the fifth, is a 3rd generation,
every 9th 3rd gen, starting from the 5th is a 4th gen, etc.

Closed form: Type12MH(k,i)
Find ith, kth Generation Type [1,2] Mersenne Hailstone
using the closed form equation

2**(6*((i-1)*9**(k-1)+(9**(k-1)-1)/2+1)-1)-1

            2**5-1  generation: 1
           2**29-1  generation: 2
          2**245-1  generation: 3
         2**2189-1  generation: 4
        2**19685-1  generation: 5
       2**177149-1  generation: 6
      2**1594325-1  generation: 7
     2**14348909-1  generation: 8
    2**129140165-1  generation: 9
   2**1162261469-1  generation:10

1.797 seconds

There is never a number too large to worry about.

>
> --
> Steven D'Aprano





More information about the Python-list mailing list