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

Christopher Barker Chris.Barker@noaa....
Tue Sep 29 16:24:50 CDT 2009

Christian Heimes wrote:
> How about using atan2()? :)

unless atan2 shortcuts for the easy ones, that doesn't strike me as
efficient (though with python function call overhead, maybe!).

Anyway, of course, some googling that I should have done in the first
place, revealed "double.py", from Martin Jansche:

http://symptotic.com/mj/code.html

double.py provides a full(?) set of IEEE functions for doubles in
Python. His solution to the problem at hand is:

def signbit(value):
"""
Test whether the sign bit of the given floating-point value is
set.  If it is set, this generally means the given value is
negative.  However, this is not the same as comparing the value
to C{0.0}.  For example:

>>> NEGATIVE_ZERO < 0.0
False

since negative zero is numerically equal to positive zero.  But
the sign bit of negative zero is indeed set:

>>> signbit(NEGATIVE_ZERO)
True
>>> signbit(0.0)
False

@type  value: float
@param value: a Python (double-precision) float value

@rtype:  bool
@return: C{True} if the sign bit of C{value} is set;
C{False} if it is not set.
"""
return (doubleToRawLongBits(value) >> 63) == 1

where:

def doubleToRawLongBits(value):
"""
@type  value: float
@param value: a Python (double-precision) float value

@rtype: long
@return: the IEEE 754 bit representation (64 bits as a long integer)
of the given double-precision floating-point value.
"""
# pack double into 64 bits, then unpack as long int
return _struct.unpack('Q', _struct.pack('d', value))[0]

Which is pretty much what I was looking for, though I can't say I've
profiled the various options at hand!

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov