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

Christopher Barker Chris.Barker at noaa.gov
Tue Sep 29 16:34:16 EDT 2009


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.

> Of course, you could always do x.__repr__().startswith('-'), but it's 
> slow, ugly, and you already said you wanted to avoid  it. 

well, that's not what I wanted to avoid -- what I wanted to avoid was 
parsing the original input string. This would be fine! I wonder if it's 
better or worse than using struct as above?

-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 at noaa.gov



More information about the NumPy-Discussion mailing list