pickle broken: can't handle NaN or Infinity under win32

Tim Peters tim.peters at gmail.com
Thu Jun 23 06:11:20 CEST 2005


[Tim Peters]
...
>> Across platforms with a 754-conforming libm, the most portable way [to
>> distinguish +0.0 from -0.0 in standard C] is via using atan2(!):
>>
>> >>> pz = 0.0
>> >>> mz = -pz
>> >>> from math import atan2
>> >>> atan2(pz, pz)
>> 0.0
>> >>> atan2(mz, mz)
>> -3.1415926535897931

[Ivan Van Laningham]
> Never fails.  Tim, you gave me the best laugh of the day.

Well, I try, Ivan.  But lest the point be missed <wink>, 754 doesn't
_want_ +0 and -0 to act differently in "almost any" way.  The only
good rationale I've seen for why it makes the distinction at all is in
Kahan's paper "Branch Cuts for Complex
Elementary Functions, or Much Ado About Nothing's Sign Bit".  There
are examples in that where, when working with complex numbers, you can
easily stumble into getting real-world dead-wrong results if there's
only one flavor of 0.  And, of course, atan2 exists primarily to help
convert complex numbers from rectangular to polar form.

Odd bit o' trivia:  following "the rules" for signed zeroes in 754
makes exponeniation c**n ambiguous, where c is a complex number with
c.real == c.imag == 0.0 (but the zeroes may be signed), and n is a
positive integer.  The signs on the zeroes coming out can depend on
the exact order in which multiplications are performed, because the
underlying multiplication isn't associative despite that it's exact. 
I stumbled into this in the 80's when KSR's Fortran compiler failed a
federal conformance test, precisely because the test did atan2 on the
components of an all-zero complex raised to an integer power, and I
had written one of the few 754-conforming libms at the time.  They
wanted 0, while my atan2 dutifully returned -pi.  I haven't had much
personal love for 754 esoterica since then ...



More information about the Python-list mailing list