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

Steven D'Aprano steve at pearwood.info
Sat Sep 29 08:19:38 EDT 2018


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

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.

A few more operations which aren't supported by floats:

NAN.numerator
NAN.denominator
NAN.from_bytes
NAN.bit_length
NAN.to_bytes



-- 
Steve


More information about the Python-ideas mailing list