[Tutor] Re: large number question
Andrei
project5 at redrival.net
Sun Sep 26 14:24:35 CEST 2004
Dick Moores wrote on Sat, 25 Sep 2004 21:08:14 -0700:
<snip>
> I'm wondering if there's a way around the apparent limit on the size of
> integers. If I enter anything larger than about 4,610,000,000,000,000,000
> (4 quintillion, 610 quadrillion in American English), I get
> "OverflowError: long int too large to convert to int"
<snip>
The nice thing is that Python shows you *exactly* where the error occurs in
a traceback, but you didn't provide that information. I think the xrange
function doesn't like very large integers. I don't know where the limit of
4.6e18 comes from though. For me it refuses to work from 2**31 onward
(2.15e9), which is basically what you'd expect on a 32-bit system.
You'll probably want to write your own iterator for this purpose instead of
using xrange, e.g. (without input validation, but you can add that if you
want it):
>>> def HugeNumberIterator(start, stop):
... i = start
... while i<stop:
... yield i
... i += 1
And then you can do something like:
>>> for i in HugeNumberIterator(int(2e35), int(2e35)+5):
... print i
<snip output>
This should work with virtually any number, as long as it fits in your
system's memory.
--
Yours,
Andrei
=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
More information about the Tutor
mailing list