[Python-Dev] Expert floats

Aahz aahz at pythoncraft.com
Tue Apr 6 10:00:53 EDT 2004


On Tue, Apr 06, 2004, Ka-Ping Yee wrote:
> On Tue, 6 Apr 2004, Simon Percivall wrote:
>>
>> So how should "2.2 - 1.2 - 1" be represented?
>>
>> Matlab (Solaris 9):          2.22044604925031e-16
>> Octave (MacOS X 10.3):       2.22044604925031e-16
>> Python 2.3.3 (MacOS X 10.3): 2.2204460492503131e-16
>>
>> Is this something you accept since Matlab does it?
> 
> I accept it, but not primarily because Matlab does it.
> 
> It is fine to show a value different from zero in this case
> because the result really is different from zero.  In this case
> those digits are necessary to represent the machine number.
> In other cases (such as 1.1) they are not.

The point is that anyone who relies on floating point will almost
certainly end up with the equivalent of Simon's case at some point.  The
most common *really* ugly case that's hard for new programmers to deal
with is:

    section_size = length / num_sections
    tmp = 0
    while tmp < length:
        process(section)
        tmp += section_size

and variations thereof.  Just because every other programming language
hides this essential difficulty is no reason to follow along.  I don't
have Tim's experience, but until we added that section to the tutorial,
I was one of the regular "first responders" on c.l.py who was always
dealing with this issue.  I stand with Uncle Timmy -- it's been much
easier now.

If you want to teach Python to complete newcomers, you need to do one of
two things:

* Avoid floating point

* Start by explaining what's going on (use Simon's example above for
emphasis)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?



More information about the Python-Dev mailing list