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

David Mertz mertz at gnosis.cx
Sun Sep 30 11:26:01 EDT 2018


On Sun, Sep 30, 2018 at 11:17 AM Steve Barnes <gadgetsteve at live.co.uk>
wrote:

> Note that my statements above had a single = i.e.
> float(NaNAwareInt('nan')) produces float('nan'), etc., as does:
>
> In [42]: nan = decimal.Decimal('nan')
> In [43]: decimal.Decimal(nan)
> Out[43]: Decimal('NaN')
> In [44]: float(nan)
> Out[44]: nan
>

I think this explanation is still a little confusing.  I take it what
you're getting at is that a "NaN" of any particular type (float, Decimal,
complex, NanAwareInt) should be a perfectly good initializer to create a
NaN of a different type using its constructor.

I think that is sensible (not sure about complex).  Currently we have:

>>> complex(nan)
(nan+0j)
>>> float(complex('nan'))
Traceback (most recent call last):
  File "<ipython-input-39-069ef735716e>", line 1, in <module>
    float(complex('nan'))
TypeError: can't convert complex to float

>>> complex(float('nan'))
(nan+0j)
>>> float(complex('nan'))
Traceback (most recent call last):
  File "<ipython-input-41-069ef735716e>", line 1, in <module>
    float(complex('nan'))
TypeError: can't convert complex to float

>>> from decimal import Decimal
>>> Decimal('nan')
Decimal('NaN')
>>> float(Decimal('nan'))
nan
>>> Decimal(float('nan'))
Decimal('NaN')
>>> complex(Decimal('nan'))
(nan+0j)
>>> Decimal(complex('nan'))
Traceback (most recent call last):
  File "<ipython-input-47-f48726d59102>", line 1, in <module>
    Decimal(complex('nan'))
TypeError: conversion from complex to Decimal is not supported


I don't think we can change the "cast-from-complex" behavior... even though
I think it maybe should have been different from the start.

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


More information about the Python-ideas mailing list