[Tutor] very odd math problem
Alan Gauld
alan.gauld at btinternet.com
Fri Mar 11 09:31:02 CET 2011
"Steven D'Aprano" <steve at pearwood.info> wrote
> Another problem: you calculate your values by repeated addition.
> This is the wrong way to do it, because each addition has a tiny
> little error, and repeating them just compounds error upon error.
> Here's an example:
>
> >>> x = 0.0
> >>> for i in range(10):
> ... x += 0.1
> ...
> >>> x == 1.0
> False
This much I follow.
> The right way is to do it like this:
>
> >>> x = 0.0
> >>> for i in range(1, 11):
> ... x = i*0.1
> ...
> >>> x == 1.0
> True
But this I don't understand.
Why would you use a loop when the final value is just
the final multiplication. Since you know the final value
in advance (you need it to create the loop!) why not
just do the final multiplication directly:
x = 10*0.1
I think I'm missing something?
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list