I just realized that what I'm doing won't work on older versions of python, anyway...
Things work fine on 2.6 Python 2.6.2 (r262:71600, Sep 3 2009, 09:36:43) [GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import struct >>> struct.pack('f', -0.0) '\x00\x00\x00\x80' <-- Has sign bit >>> struct.pack('>f', -0.0) '\x80\x00\x00\x00' <-- Has sign bit >>> struct.pack('<f', -0.0) '\x00\x00\x00\x80' <-- Has sign bit >>> struct.pack('=f', -0.0) '\x00\x00\x00\x80' >>> But on python 2.3 it only works for the native endian case Python 2.3.4 (#1, Jan 9 2007, 16:40:09) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import struct >>> struct.pack('f', -0.0) '\x00\x00\x00\x80' <-- Correct, has sign bit and unpacks to -0.0 >>> struct.pack('>f', -0.0) '\x00\x00\x00\x00' <-- (big-endian) Lost the sign bit! Unpacks to 0.0 >>> struct.pack('<f', -0.0) '\x00\x00\x00\x00' <-- (big-endian) Lost the sign bit! >>> struct.pack('=f', -0.0) '\x00\x00\x00\x00' <-- (whichever is native) Lost the sign bit! >>> So I guess prior to 2.6 (which as xavier pointed out, has copysign) trying to use struct won't be endian-independent. Of course, you could always do x.__repr__().startswith('-'), but it's slow, ugly, and you already said you wanted to avoid it. On the other hand, it works everywhere with any version of python. At any rate, I'm doubt I'm helping that much, -Joe On Tue, Sep 29, 2009 at 12:19 PM, Joe Kington <jking...@wisc.edu> wrote: > 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.bar...@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.bar...@noaa.gov >> _______________________________________________ >> NumPy-Discussion mailing list >> NumPy-Discussion@scipy.org >> http://mail.scipy.org/mailman/listinfo/numpy-discussion >> > >
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion