[Numpy-discussion] making the distinction between -0.0 and 0.0..

Robert Kern robert.kern@gmail....
Tue Sep 29 15:48:10 CDT 2009

On Tue, Sep 29, 2009 at 15:34, Christopher Barker <Chris.Barker@noaa.gov> wrote:
> Joe Kington wrote:
>> I just realized that what I'm doing won't work on older versions of
>> python, anyway...
> What I was looking for was which actual bit the sign bit is, as
> expressed as a native integer, so I can do a bitwise_and.
> But now that I think about it, I only need to test zero, not all
> numbers, so I should be able to do:
> def signbit(x):
>     if x < 0.0:
>         return True
>     elif x == 0.0:
>         if struct.pack('d',x) == struct.pack('d',-0.0):
>             return True
>     else
>         return False
> Fortunately, this isn't performance critical.

To speed it up some, precalculate the struct.pack('d', -0.0) constant
outside of the function.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list