[Python-Dev] Minutes from the Numeric Coercion dev-day session
Tim Peters
tim.one@home.com
Wed, 14 Mar 2001 01:01:24 -0500
[Ka-Ping Yee]
> I'll argue now -- just as i argued back then, but louder! -- that
> this isn't necessary. repr(1.1) can be 1.1 without losing any precision.
>
> Simply stated, you only need to display as many decimal places as are
> necessary to regenerate the number. So if x happens to be the
> floating-point number closest to 1.1, then 1.1 is all you have to show.
>
> By definition, if you type x = 1.1, x will get the floating-point
> number closest in value to 1.1.
This claim is simply false unless the platform string->float routines do
proper rounding, and that's more demanding than even the anal 754 std
requires (because in the general case proper rounding requires bigint
arithmetic).
> So x will print as 1.1.
By magic <0.1 wink>?
This *can* work, but only if Python does float<->string conversions itself,
leaving the platform libc out of it. I gave references to directly relevant
papers, and to David Gay's NETLIB implementation code, the last time we went
thru this. Note that Gay's code bristles with platform #ifdef's, because
there is no portable way in C89 to get the bit-level info this requires.
It's some of the most excruciatingly delicate code I've ever plowed thru. If
you want to submit it as a patch, I expect Guido will require a promise in
blood that he'll never have to maintain it <wink>.
BTW, Scheme implementations are required to do proper rounding in both
string<->float directions, and minimal-length (wrt idempotence) float->string
conversions (provided that a given Scheme supports floats at all). That was
in fact the original inspiration for Clinger, Steele and White's work in this
area. It's exactly what you want too (because it's exactly what you need to
make your earlier claims true). A more recent paper by Dybvig and ??? (can't
remember now) builds on the earlier work, using Gay's code by reference as a
subroutine, and speeding some of the other cases where Gay's code is slothful
by a factor of about 70.
scheme-does-a-better-job-on-numerics-in-many-respects-ly y'rs - tim