PEP0238 lament

Tim Peters tim at digicool.com
Mon Jul 23 19:06:43 CEST 2001


[Tim Hochberg]
> ...
> The fact that oponents of this proposal keep suggesting methods for
> doing this that are wrong only reveals that this is more difficult
> than generally supposed.
>
> ...
>
> A more correct solution suggested by Konrad is:
>
> def simple_func(x, y):
>    return y / (x+0.0)**2
>
> This is correct, but can hardly be called simple or clear.

In this specific case "x + 0.0" is correct, but in general it isn't:  on
boxes with 754 arithmetic (virtually everything), x+0. has a different sign
bit than x when x is -0.0, and the default rounding mode is in effect.  In
this specific case that doesn't matter because the result is immediately
squared, but in general you can't know that it doesn't matter.  Safest is

    x*1.0

which works in all cases -- maybe.  There's still hardware out there that
flushes denormal results to 0, and on such boxes both x+0. and x*1.0 can
return 0 when x is not 0.  Python inherits such behaviors from the platform.

So "this is more difficult than generally supposed" applies even to people
who know quite a bit about this stuff.

correctness-is-a-severe-requirement-ly y'rs  - tim





More information about the Python-list mailing list