[Numpy-discussion] What is the sign of nan?

Pete Forman pete.forman@westerngeco....
Tue Sep 30 08:42:18 CDT 2008


"Charles R Harris" <charlesr.harris@gmail.com> writes:

 > OK, here is what is looks like to me at the moment given that numpy
 > requires an IEEE754 machine:
>
 >    o We need a reliable value for NAN. [...]
 >      
 >
 >    o Max/min follow the IEEE standard. Given a choice of
 >      nan/non-nan, return non-nan. [...]

Yes, that follows 754r and C99.

 >    o Signbit returns the value of the signbit function, but nonzero
 >      values are set to 1.

Looks okay to me.

 >    o I am unsure of sign. Should it return signed zeros? Should it
 >      return nan for nan or return the sign of the nan? I am
 >      inclined towards returning nan.

How is sign used?  If it is in x * sign(y) then it might be better to
use copysign(x, y) which is well defined even with signed zeros and
NaNs.  It depends on whether you want special behavior when y is zero.
In copysign y being 0 or +0 is considered positive, so x is returned.

So you could use this as a specification.

def sign(y):
    if y == 0: # True for -0 and +0 too
        return 0 # or perhaps return y
    else
        return copysign(1, y)

Your inclination leads to this.

def sign(y):
    if y == 0 or isnan(y):
        return y
    else
        return copysign(1, y)

The better choice will be governed by how sign is used in practice.
-- 
Pete Forman                -./\.-  Disclaimer: This post is originated
WesternGeco                  -./\.-   by myself and does not represent
pete.forman@westerngeco.com    -./\.-   the opinion of Schlumberger or
http://petef.22web.net           -./\.-   WesternGeco.



More information about the Numpy-discussion mailing list