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

David Mertz mertz at gnosis.cx
Sun Sep 30 10:57:58 EDT 2018

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

> > int.bit_length() is stored as a system-native integer, e.g. 64-bit,
> rather than recursively as a Python int.  So the largest Python int is
> '2**sys.maxsize` (e.g. '2**(2**63-1)').  I may possibly have an off-by-one
> or off-by-power-of-two in there :-).
> Hah. Is that a fundamental limit based on the underlying
> representation, or would it mean that bit_length would bomb with an
> exception if the number is larger than that?

It's implementation specific.  In concept, a version of Python other than
CPython 3.7 could store bit-length as either a Python int or a
system-native int, to whatever recursive depth was needed to prevent
overflows.  Or perhaps as a linked list of native ints. Or something else.

There's no sane reason to bother doing that, but there's never been a
*promise* in Python semantics not to represent numbers with more than 1e19
bits in them.

> 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.

I'm also not sure if you'll see this error before or after the heat death
of the universe ;-).

I *am* sure that your swap space on your puny few terabyte disk will fill
up before you complete the calculation, so that might be a system level
crash not a caught exception.

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/8ce033ae/attachment.html>

More information about the Python-ideas mailing list