[Python-ideas] Suggestion: Extend integers to include iNaN
Steve Barnes
gadgetsteve at live.co.uk
Sun Sep 30 11:31:36 EDT 2018
On 30/09/2018 16:26, David Mertz wrote:
> On Sun, Sep 30, 2018 at 11:17 AM Steve Barnes <gadgetsteve at live.co.uk
> <mailto: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.
>
No complex can be converted to float without accessing either the real
or imag component.
In [51]: cn=complex(4, float('nan'))
In [52]: cn
Out[52]: (4+nanj)
In [53]: cn.real
Out[53]: 4.0
In [54]: cn.imag
Out[54]: nan
In [55]: float(cn.imag)
Out[55]: nan
--
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect
those of my employer.
---
This email has been checked for viruses by AVG.
https://www.avg.com
More information about the Python-ideas
mailing list