no sign() function ?

Mark Dickinson dickinsm at gmail.com
Tue Dec 23 11:25:49 EST 2008


On Dec 23, 2:59 pm, Christian Heimes <li... at cheimes.de> wrote:
> 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.

I think that's not quite true:  NaNs have a sign;  it's just not
accorded any particular meaning by IEEE 754---in particular,
the standard says nothing about the sign of a NaN result
for most arithmetic operations. However, the sign bit of
a NaN does play its usual role in operations like
copysign, abs, negation.  For example, here's a Python
2.6 session on OS X, where the 'default' nan is negative,
in the sense that its sign bit is set:

>>> nan = float('nan')
>>> from math import copysign
>>> copysign(5.0, nan)
-5.0
>>> copysign(5.0, -nan)
5.0
>>> copysign(5.0, abs(nan))
5.0

Mark



More information about the Python-list mailing list