On Sat, Dec 28, 2019 at 3:40 PM David Mertz <mertz@gnosis.cx> wrote:
What about Decimal snan?

That version tries to call .is_nan() first, so works fine with the Decimal snan. 

But as Nark pointed out, having an s an raise maybe the right thing to do anyway— it should not be used to indicate a missing value anyway.

I’m pretty sure the issue with numpy array scalars is that it’s not calling __float__.

Which does make think that maybe all __float__ implementations should return a float NaN when appropriate.

-CHB






On Sat, Dec 28, 2019, 5:53 PM Christopher Barker <pythonchb@gmail.com> wrote:
On Sat, Dec 28, 2019 at 2:41 AM Antoine Pitrou <solipsis@pitrou.net> wrote:
+1 for a .is_nan() method on suitable types.  That's the most natural
and elegant solution, IMHO.  Tricks like "x == x" are nice when you
*know* that x is a float or Decimal, but not in the general case.

agreed -- while it may work in almost all cases, what it is really checking is whether an object compares to itself, which is not question being asked.

I suppose we could do something like:

def is_nan(num):
    try:
        return num.is_nan()
    except AttributeError:
        if isinstance(num, Number):
            return not (num == num)
        else:
            return False

Running it on my test code, it works for everything I thought to test except numpy arrays of size 1. 

-CHB


-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython