Long int too large to convert?

fbasegmez fb at ultranet.com
Thu Sep 13 09:15:35 EDT 2001


Markus Schaber <markus at schabi.de> wrote in message news:<11116625.PYv0bhDfK2 at lunix.schabi.de>...
> Hi,
> 
> fbasegmez <fb at ultranet.com> schrub:
> >>>> for i in xrange(1L, 5 * pow(10L, 40)):
> > ...   x = i * x
> > ...
> > Traceback (most recent call last):
> >   File "<interactive input>", line 1, in ?
> > OverflowError: long int too large to convert
> > Any ideas?
> 
> The Problem seems to be that xrange only accepts values in the range of 
> integers. Pow(10L, 40) = 10000000000000000000000000000000000000000 (one 
> could also write 10**40L) just is too big.
> 
> Your example, assuming you can run it with one loop per processor clock 
> tick (which would assume a that you have a highly optimizing compiler, 
> and that you can in parallel add and multiply long ints in one cycle), 
> running at 1 GigaHertz clock speed, would - roughly estimated - still 
> need about 1585489599188229325215625 years to compute.
> 
> Not even thinking about storing the result in a computers memory.
> 
> And in my python documentation, I find the info that xrange " is very 
> similar to range()". And about range, it says "The arguments must be 
> plain integers.".
> 
> Personally, I think it doesn't make sense to have ranges and xranges 
> that need long integers for displaying their length. (Ranges of such 
> size wouldn't even have enough place in today's 32 bit machines memory, 
> and xranges would need a looong time to loop through.)
> 
> But there may be reasons to open ranges and xranges for long ints, such 
> that you can e. G. define an xrange(10**40, 2*(10**40), 10**39) which 
> has 10 elements - maybe this comes "automagically" with the int/long 
> unification.
> 
> > First I was going to use scientific notation instead of pow(x, y) but
> > then I decided not to because,
> >>>> long(5e40)
>  50000000000000000310004322520389159747584L
> >>>>
> > I am aware of numerical precision issues but this still managed to
> > surprise me.
> 
> Scientific notation uses floats, which have only a limited number of 
> significant digits (the number was between 15 and 17, AFAIR) and are 
> stored as a mantissa and an exponent in base2 internally.
> 
> 10**40 is about 2**132.877123795. So as long as you have less then 133 
> bits for the mantissa, the computer can only approximate it, the lower 
> bits are lost. (And usual IEEE double precision format has 64 bits for 
> exponent, mantissa and sign)
> 
> I hope I could clear up some of the mysteries of large numbers - if 
> not, don't refuse to ask again :-)
> markus

Let me try to give you a little bit more details about the steps I
went through so that I can give you some feedback.  I am familiar with
programming but not functional programming and relatively new to
Python (I love it).  I was looking at the map, reduce, lamdba and
filter functions and started playing with them.
First I did this:
>>> def factorial(int):
... 	return reduce(lambda x, y: x*y, range(1, int+1))
... 
>>> factorial(10)
3628800
>>> 

Then I realized that this would fail for
>>> factorial(30)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<interactive input>", line 2, in factorial
  File "<interactive input>", line 2, in <lambda>
OverflowError: integer multiplication
>>> 

This is the type of overflow message I am familiar with.  
I thought I had two options to correct my first program. If you have
an integer and a long integer result should be long. Like,

>>> 999999999 * 999999999L
999999998000000001L

So, I thought the long number can come from either the lamba or the
range.

1-
>>> def factorial(int):
... 	return reduce(lambda x, y: long(x*y), range(1, int+1))
... 
>>> factorial(40)
815915283247897734345611269596115894272000000000L
Works fine.

2-
>>> def factorial(int):
... 	return reduce(lambda x, y: x*y, range(1L, int+1L))
... 
>>> factorial(10)
362880
>>> factorial(50)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<interactive input>", line 2, in factorial
  File "<interactive input>", line 2, in <lambda>
OverflowError: integer multiplication

And I remembered something like xrange being used for large integers
so I tried

>>> for i in xrange(1L, 10L):
... 	print i,
... 	
1 2 3 4 5 6 7 8 9
>>> 

I thought the xrange could handle the long integers. Then I tested it

>>> for i in xrange(eval(200*'9'+'L'), eval('1'+200*'0'+'L')):
... 	print i
... 	
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
OverflowError: long int too large to convert

My first question was; too large to convert to what?  Now I realize
(thanx to your response) that range or xrange is trying to convert the
long integers to integers.  I think I would be less surprised if I got

>>> for i in xrange(1L, 10L):
... 	print i,
... 
Error. xrange start and stop values must be integer.  Or something
like that.

Just some feedback.

Fahri



More information about the Python-list mailing list