PEP 238 (revised)

Tim Peters tim.one at home.com
Fri Jul 27 16:59:01 EDT 2001


[Guido]
> IEEE 754, the floating point standard in use in virtually all current
> hardware, mandates that -0.0 be distinguishable from 0.0, in order to
> be able to represent the sign of degenerated underflow results.

That's part of the reason.  Another is to distinguish "which side" of a
branch cut you're on when slinging functions using complex arithmetic.  One
more is for careful support of interval arithmetic endcases.  These are
related in my mind.

> (Or so I understand, having been educated in IEEE 754 through numerous
> discussions with Tim Peters, but not by reading the standard.  If I'm
> wrong, Tim will surely correct me. :-)

But so gently it's hard to notice you've been dissed <wink>.

> I don't know of a portable way to generate -0.0 in Python; on my Linux
> box, simply writing -0.0 does it, but not on my Windows box.

You're almost certainly wrong about that, and this illustrates the
difficulty of doing anything with 754 arithmetic in Python that doesn't
intersect with Fortran's view of the world circa 1960 (and so adopted
wholesale by C89 too, 30 years later).

One problem is that there's no cross-platform consistency in whether C's
printf will *display* the sign of a 0.  Here on my Windows box:

>>> x = -0.0
>>> x
0.0
>>>

But you can't actually tell anything from that, because the Windows C printf
suppresses the sign of a 0.  You can't tell by trying to compute the
reciprocal either (to look at the sign of the resulting infinity), because
*Python* stops you from doing that:

>>> 1/x
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ZeroDivisionError: float division
>>>

Comparison is also hopeless (but by design:  +0 compares equal to -0):

>>> x == 0.0
1
>>>

Here's one way to tell:

>>> import math
>>> math.atan2(x, x)
-3.1415926535897931
>>> math.atan2(0, 0)
0.0
>>>

That is, I really did get minus 0 on Windows!  BTW, that also hints at why
signed zeroes can be a life-saver when doing complex arithmetic.  However,
whether atan2() handles signed zeroes correctly also varies across
platforms.

full-754-is-still-unusable-in-practice-ly y'rs  - tim





More information about the Python-list mailing list