[Python-Dev] Expert floats

Ka-Ping Yee python-dev at zesty.ca
Tue Apr 6 08:46:44 EDT 2004


On Tue, 30 Mar 2004, Tim Peters wrote:
> I want marshaling of fp numbers to give exact (not approximate) round-trip
> equality on a single box, and across all boxes supporting the 754 standard
> where C maps "double" to a 754 double.

That is a valuable property.  I support it and support Python continuing
to have that property.

I hope it has been made quite clear by now that this property does not
constrain how numbers are displayed by the interpreter in human-readable
form.  The issue of choosing an appropriate string representation of a
number is unaffected by the desire for the above property.

> [Ping]
> > All right.  Maybe we can make some progress.
>
> Probably not -- we have indeed been thru all of this before.

I think we *have* made progress.  Now we can set aside the red-herring
issue of platform-independent serialization and focus on the real issue:
human-readable string representation.

So let's look at what you said about Python's accessibility:

> As I said before (again and again and again <wink>), I'm the one who has
> fielded most newbie questions about fp since Python's beginning, and I'm
> very happy with the results of changing repr() to produce 17 digits.  They
> get a little shock at the start now, but potentially save themselves from
> catastrophe by being forced to grow some *necessary* caution about fp
> results early.

Now you are pulling rank.  I cannot dispute your longer history and
greater experience with Python; it is something i greatly admire and
respect.  I also don't know your personal experiences teaching Python.

But i can tell you my experiences.  And i can tell you that i have
tried to teach Python to many people, individually and in groups.  I
taught a class in Python at UC Berkeley last spring to 22 people who
had never used Python before.  I maintained good communication with
the students and their feedback was very positive about the class.

How did the class react to floating-point?  Seeing behaviour like this:

    >>> 3.3
    3.2999999999999998
    >>>

confused and frightened them, and continues to confuse and frighten
almost everyone i teach.  (The rare exceptions are the people who have
done lots of computational work before and know how binary floating-point
representations work.)  Every time this happens, the teaching is
derailed and i am forced to go into an explanation of binary
floating-point to assuage their fears.

Remember, i am trying to teach basic programming skills.  How to solve
problems; how to break down problems into steps; what's a subroutine;
and so on.  Aside from this floating-point thing throwing them off,
Python is a great first language for new programmers.  This is not the
time to talk about internal number representation.

I am tired of making excuses for Python.  I love to tell people about
Python and show them what it can do for them.  But this floating-point
problem is embarrassing.  People are confused because no other system
they've seen behaves like this.  Other languages don't print their
numbers like this.  Accounting programs and spreadsheets don't print
their numbers like this.  Matlab and Maple and Mathematica don't print
their numbers like this.  Only Python insists on being this ugly.  And
it screws up the most common way that people first get to know Python
-- as a handy interactive calculator.

And for what?  For no gain at all -- because when you limit your focus
to the display issue, the only argument you're making is "People should
be frightened."  That's a pointless reason.

Everything in Python -- everything in computers, in fact -- is a *model*.
We don't expect the model to be perfectly accurate or to be completely
free of limitations.  IEEE 754 happens to be the prevalent model for
the real number line.  We don't print every string with a message after
it saying "WARNING: MAXIMUM LENGTH 4294967296", and we shouldn't do
the same for floats.

"3.2999999999999998" does not give you any more information than "3.3".
They both represent exactly the same value.  "3.3" is vastly easier to
read.  The only reason you seem to want to display "3.2999999999999998"
is to frighten people.  So why not display "3.3 DANGER DANGER!!"?
Even that would be much easier to read, but my point is that i hope it
exposes the problem with your argument.

I'm asking you to be more realistic here.  Not everyone runs into
floating-point corner cases.  In fact, very few people do.  I have never
encountered such a problem in my entire history of using Python.  And if
you surveyed the user community, i'm sure you would find that only a
small minority cares enough about the 17th decimal place for the
discrepancy to be an issue.

Now, why should that minority make it everyone else's problem?

Is this CP4E or CPFPFWBFP?  Computer Programming For Everybody?  Or
Computer Programming For People Familiar With Binary Floating-Point?

You say it's better for people to get "bitten early".  What's better:
everyone suffering for a problem that will never affect most of them,
or just those who care about the issue having to deal with it?

    Beautiful is better than ugly.
    Practicality beats purity.
    Readability counts.



-- ?!ng



More information about the Python-Dev mailing list