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

Joe Kington jkington@wisc....
Tue Sep 29 12:19:53 CDT 2009


Well, this is messy, and nearly unreadable, but it should work and is pure
python(and I think even be endian-independent).

struct.unpack('b',struct.pack('>d', X)[0])[0] >= 0
(where X is the variable you want to test)

In [54]: struct.unpack('b',struct.pack('>d',0.0)[0])[0] >= 0
Out[54]: True

In [55]: struct.unpack('b',struct.pack('>d',-0.0)[0])[0] >= 0
Out[55]: False

In [56]: struct.unpack('b',struct.pack('>d',-0.0000001)[0])[0] >= 0
Out[56]: False

In [57]: struct.unpack('b',struct.pack('>d',0.0000001)[0])[0] >= 0
Out[57]: True

In [58]: struct.unpack('b',struct.pack('>d',3999564.8763)[0])[0] >= 0
Out[58]: True

In [59]: struct.unpack('b',struct.pack('>d',-3999564.8763)[0])[0] >= 0
Out[59]: False

Hope that helps, anyway
-Joe

On Tue, Sep 29, 2009 at 12:04 PM, Christopher Barker
<Chris.Barker@noaa.gov>wrote:

> Pauli Virtanen wrote:
> > Tue, 29 Sep 2009 09:53:40 -0700, Christopher Barker wrote:
> > [clip]
> >> How can I identify -0.0?
> >
> > signbit
> >
>
> perfect for numpy, but at this point I don't have a numpy dependency
> (very unusual for my code!). Anyone know a pure-python way to get it?
>
> It seems I should be able to do something like:
>
> struct.pack("d",-3.4)[0] & Something
>
> but I'm not sure what "Something" is, and it would be endian-dependent,
> wouldn't it?
>
> thanks,
> -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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090929/076600aa/attachment-0001.html 


More information about the NumPy-Discussion mailing list