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

Serhiy Storchaka storchaka at gmail.com
Sat Sep 29 08:56:40 EDT 2018

29.09.18 15:19, Steven D'Aprano пише:
> On Sat, Sep 29, 2018 at 10:50:24AM +0300, Serhiy Storchaka wrote:
>>>> How does it differ from float('nan')?
>>> It is still an integer and would pass through any processing that
>>> expected an integer as one, (with a value of iNaN).
>> Python is dynamically typed language. What is such processing that would
>> work with iNaN, but doesn't work with float('nan')?
> The most obvious difference is that any code which checks for
> isinstance(x, int) will fail with a float NAN.

Yes, an explicit check. But why do you need an explicit check? What will 
you do with True returned for iNaN? Can you convert it to a machine 
integer or use it as length or index?

> If you use MyPy for
> static type checking, passing a float NAN to something annotated to only
> accept ints will be flagged as an error.

I think that passing iNaN to most of functions which expect int is an 
error. Does MyPy supports something like "int | iNaN"? Than it should be 
used for functions which accept int and iNaN.

> Bitwise operators don't work:
> py> NAN = float("nan")
> py> NAN & 1
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for &: 'float' and 'int'
> Now I'm not sure what Steve expects NANs to do with bitwise operators.
> But raising TypeError is probably not what we want.

Since these operations make no sense, it makes no sense to discuss them.

> A few more operations which aren't supported by floats:
> NAN.numerator
> NAN.denominator

Do you often use these attributes of ints?

> NAN.from_bytes > NAN.bit_length
> NAN.to_bytes

What is the meaning of this?

More information about the Python-ideas mailing list