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

Jonathan Fine jfine2358 at gmail.com
Mon Oct 1 08:02:58 EDT 2018

Hi Steve

You've suggested that we add to Python an integer NaN object, similar
to the already existing float NaN object.

>>> x = float('nan')
>>> x, type(x)
(nan, <class 'float'>)

I've learnt that decimal also has a NaN object, but not fractions.

>>> from decimal import Decimal
>>> y = Decimal('nan')
>>> y, type(y)
(Decimal('NaN'), <class 'decimal.Decimal'>)

Numpy has its own fixed size int classes

>>> from numpy import int16
>>> x = int16(2358); x, type(x)
(2358, <class 'numpy.int16'>)
>>> x = x * x; x, type(x)
__main__:1: RuntimeWarning: overflow encountered in short_scalars
(-10396, <class 'numpy.int16'>)

I'm confident that one could create classes similar to numpy.int16, except that

>>> z = int16('nan')
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: 'nan'

would not raise an exception (and would have the semantics you wish for).

I wonder, would this be sufficient for the use cases you have in mind?

More information about the Python-ideas mailing list