[Python-Dev] Expert floats
Andrew Koenig
ark-mlist at att.net
Tue Mar 30 13:16:27 EST 2004
> If typing in "1.1" produces x, then "1.1" is a perfectly accurate
> representation of x on the current platform. And that is sufficient.
>
> Showing "1.1000000000000001" is a clear case of confusing lots of people
> in exchange for an obscure benefit to very few. If i could vote for
> just one thing to roll back about Python, this would be it.
I wish that Python would use the same conversion rules as Scheme:
string->float yields the closest rounded approximation to the
infinite-precision number represented by the string
float->string yields the string with the fewest significant
digits that, when converted as above, yields exactly the same
floating-point value
These rules guarantee that 1.1 will always print as 1.1, and also that
printing any floating-point value and reading it back in again will give
exactly the same results. They do, however, have three disadvantages:
1) They are a pain to implement correctly.
2) There are some pathological cases that take a long time to
convert.
3) Results may be different from the local C implementation.
(1) can be ameliorated, on many platforms by using David Gay's
implementation (www.netlib.org/fp, which is distributed for free under such
liberal terms that I find it hard to believe that it wouldn't be compatible
with Python). I don't know what to do about (2) or (3).
More information about the Python-Dev
mailing list