no sign() function ?

Christian Heimes lists at cheimes.de
Tue Dec 23 15:59:36 CET 2008


All algorithm including my own suffer from one mistake. Nobody accounts
for NaN (not a number). You have to check for NaNs, too. NaNs have no
sign at all.

You could also try to do some fancy bit mask operation like

>>> ord(struct.pack("d", 0.)[7]) & 0x80
0
>>> ord(struct.pack("d", -0.)[7]) & 0x80
128

But you have to take care of little endian, big endian and mixd endian
IEEE 754 platforms, too. There are also platforms that don't have IEEE
754 floats at all ... Have fun :)

Christian




More information about the Python-list mailing list