float("nan") in set or as key
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon May 30 02:13:31 EDT 2011
On Mon, 30 May 2011 04:29:19 +0000, Chris Torek wrote:
> In article <4de31635$0$29990$c3e8da3$5496439d at news.astraweb.com>, Steven
> D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
>>That's also completely wrong. The correct way to test for a NAN is with
>>the IEEE-mandated function isnan(). The NAN != NAN trick is exactly
>>that, a trick, used by programmers when their language or compiler
>>doesn't support isnan().
>
> Perhaps it would be reasonable to be able to do:
>
> x.isnan()
>
> when x is a float.
Better than a float method is a function which takes any number as
argument:
>>> import math, fractions, decimal
>>> math.isnan(fractions.Fraction(2, 3))
False
>>> math.isnan(decimal.Decimal('nan'))
True
You can even handle complex NANs with the cmath module:
>>> import cmath
>>> cmath.isnan(complex(1, float('nan')))
True
--
Steven
More information about the Python-list
mailing list