[Tutor] math question

Karl Pflästerer sigurd at 12move.de
Fri Apr 23 18:01:51 EDT 2004


On 23 Apr 2004, Alan Gauld <- alan.gauld at blueyonder.co.uk wrote:

>> should see the code and know a difference between adding
>> .33 + .33 + .333 and 1/3 + 1/3 + 1/3

> The only way the computer can do that is to parse the values,
> apply some expert system rules:

No.  The above could be solved easily if Python knew how to handle
rational numbers (IIRc there has been some discussion about it on clp
and there may also have been a PEP but I'm not sure).

First in Python:

>>> 1/3
0.33333333333333331
>>> _ * 2
0.66666666666666663

That's what we know; rationals are coerced into floats (with all
problems).

Now the same in Scheme:

> (/ 1 3)
1/3
> (* 1/3 2)
2/3
> 

So if Python had rationals some problems with floats would be easier (of
course irrational numbers like sqrt(2) still couldn't be stored exactly
and for the question of the OP we needed symbolic computing).

> If the inverse of a number, x, is added to itself x times
> then the result is x.

With rationals no problem.


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list