[Tutor] math question

yak fuzzydav at wcoil.com
Fri Apr 23 13:15:22 EDT 2004


On Fri, 23 Apr 2004 07:53:03 -0800
Chris Lott <chris at chrislott.org> wrote:

> 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
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

Heres something interesting:

>>> print math.sqrt(2) * math.sqrt(2)
2.0
>>> math.sqrt(2) * math.sqrt(2)
2.0000000000000004
>>> print math.sqrt(2) ** 2
2.0
>>> math.sqrt(2) ** 2
2.0000000000000004
>>>

I'm not sure how it gets from the internal representation to what is printed though. 



More information about the Tutor mailing list