[Python-ideas] Suggestion: Extend integers to include iNaN

David Mertz mertz at gnosis.cx
Sun Sep 30 11:10:23 EDT 2018


On Sun, Sep 30, 2018 at 11:04 AM Chris Angelico <rosuav at gmail.com> wrote:

> On Mon, Oct 1, 2018 at 12:58 AM David Mertz <mertz at gnosis.cx> wrote:
> >> I'm not sure what's going on. I have a Py3 busily calculating
> >> 2**(2**65) and it's pegging a CPU core while progressively consuming
> >> memory, but it responds to Ctrl-C, which suggests that Python bytecode
> >> is still being executed.
> > I'm not quite sure, but my guess is that at SOME POINT you'll get an
> overflow exception when the current value gets too big to store as a native
> int.  Or maybe it'll be a segfault; I don't know.
> >>> "a" * (2**63 - 1)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> MemoryError
>
> Bam, instant. (Interestingly, trying to use 2**63 says "OverflowError:
> cannot fit 'int' into an index-sized integer", suggesting that
> "index-sized integer" is signed, even though a size can and should be
> unsigned.) Were there some kind of hard limit, it would be entirely
> possible to exceed that and get an instant error, without actually
> calculating all the way up there. But it looks like that doesn't
> happen.
>

Sure, it wouldn't be THAT hard to do bounds checking in the Python
implementation  to make '2**(2**65))' an instance error rather than a
wait-to-exhaust-swap one.  But it's a corner case, and probably not worth
the overhead for all the non-crazy uses of integer arithmetic.


> In any case, the colloquial definition that I usually cite ("Python
> can store infinitely big integers" or "integers can be as big as you
> have RAM to store") is within epsilon of correct :D
>

I teach the same thing.  For beginners or intermediate students, I just say
"unbounded ints." Occasionally for advanced students I add the footnote.


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180930/4daf23bc/attachment.html>


More information about the Python-ideas mailing list