[Matrix-SIG] NaN (almost)

Tim Peters tim_one@email.msn.com
Wed, 7 Oct 1998 23:55:25 -0400


[Hoon Yoon, trying to identify NaNs]
> ...
> >>> NaNv = 1e1000000 - 1e10000000
> ...
> No wonder
>
> equal(NaNv, NaNv)  is 0 too:

That's interesting!  That's what the IEEE-754 standard says NaN == NaN
should do (believe it or not <wink>), but in core Python NaNv == NaNv
returns 1 (core comparisons test for object identity (and treat "x is y" as
"equal") before delegating to the type-specific comparison implementations).

I don't have Numeric here to test it, but the behavior you show above
suggests that the standard-approved NaN test

    x is a NaN

if and only if

    not x == x

should work reliably for you using Numeric's "equal".  I.e., compare the
array to itself elementwise using equal, and the result will have 1's all &
everywhere a NaN isn't <wink>.

Using core Python ==, I don't think you can do anything *simple* better than

    x is a NaN

if and only if

    not x + 0 == x

But that can fail (by saying something's a NaN that actually isn't) if x is
a denorm and you're on flush-denorm-to-0 hardware.

not-sure-that's-helpful-but-it's-sure-accurate<wink>-ly y'rs  - tim