don't NaN & infinities hide FP errors

Tim Peters tim.peters at gmail.com
Sat Nov 20 23:31:39 EST 2004


[Jive]
> I kind of like the various flavors of NaN.  I have on occasion written
> numerical code that exploits them.  But seeing as how Python aspires
> to some level of cross-platform portability, it really should throw
> exceptions rather than return NaN's, Inf's, and the lot.  I am surprised
> that it doesn't always do that.

Python is written to the C89 standard.  That standard says nothing
about NaNs, infs, signed zeroes, or subnormals, and says little about
exceptional numeric conditions.  C99 goes on to promise almost nothing
about exceptional conditions in libm functions; it says a lot about
754 fp semantics, but leaves 754 conformance optional.  As a result,
there's no consistency across vendor C compilers or runtimes wrt these
gimmicks, neither in basic arithmetic nor in libm behavior.

If someone wants to contribute portable C89 code to get at all that
stuff in all cases, then (a) fine; (b) they wouldn't be the first to
try it; and (c) they would be the first not to give up <wink>.

The implementation of the new decimal module does arithmetic entirely
via Python-coded emulation, so all its behavior is under our control. 
As a result, it fully conforms, on all platforms, to the proposed IBM
standard for decimal arithmetic, including the IEEE 854 subset wrt
exceptional semantics, values (infs, NaNs, etc), status flags, and
trap-enable flags.  Also as a result, it may run about 100x slower
than your binary fp HW.



More information about the Python-list mailing list