[Tutor] math question

Chris Lott chris at chrislott.org
Fri Apr 23 11:53:03 EDT 2004


Karl Pflästerer wrote:

> Here are several problems:
> (a) sqrt(2) can't be written as exact value (neither base 10 nor base
>     2).  So Python uses a floating point approximation
> (b) you see the internal representation of the numbers (repr())
> (c) most floating point numbers can't be represented exactly in the way
>     Python stores them
> 
> A very good explanation of the problems with floating point numbers can
> be found in the Python tutorial in the official documentation.  You
> should read it.
> 
> A simple example from it which shows the problem:

I understand and I don't understand. I've read that section in the 
tutorial. I understand that the sqrt(2) can't be written as an exact 
value. But the sqrt(2)^2 certainly can be written in an exact value. It 
seems strange to me (I'm no mathematician and I suspect this is a 
philosophy question rather than a Python question) that the language 
can't figure this out-- similarly, if I add up 1/3 + 1/3 + 1/3 there is 
no reason (to my layperson's mind) that it doesn't come out to 1, 
regardless of the individual values. I guess it seems like something 
should see the code and know a difference between adding .33 + .33 + 
.333 and 1/3 + 1/3 + 1/3

I can't conceive of why it would *matter* in anything I do, but I had a 
hard time explaining to my daughter when I was helping her with a 
math/geometry problem and showed her how to use the Python shell as a 
kind of calculator and was demonstrating to her and got 2.000000004 here :)

c
--
Chris Lott



More information about the Tutor mailing list