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

Charles R Harris charlesr.harris at gmail.com
Tue Sep 30 15:04:41 EDT 2008


On Tue, Sep 30, 2008 at 7:42 AM, Pete Forman <pete.forman at westerngeco.com>wrote:

> "Charles R Harris" <charlesr.harris at 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)
>

I'm leaning towards the first at the moment. I would prefer the signed zero
also, but that might actually break some code so probably the safe near term
choice is the unsigned zero.

For max/min I am going to introduce new ufuncs, fmax/fmin, which return
numbers unless both arguements are nan. The current maximum/minimum
functions will return nan if either arguement is a nan. How these might
integrated into the max/min ndarray methods can be left to the future.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080930/2fe0318a/attachment.html>


More information about the NumPy-Discussion mailing list