[Tutor] very odd math problem
davea at ieee.org
Fri Mar 11 12:40:13 CET 2011
On 01/-10/-28163 02:59 PM, Alan Gauld wrote:
> "Steven D'Aprano" <steve at pearwood.info> wrote
>> 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
> 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?
What you missed was the original context, where other work was being
done in the loop, and where the accuracy of the "accumulator" was being
misunderstood. Steven's point was that doing repeated sums of a
quantized value is going to lead to a cumulative error, which can be
minimized by using integers and scaling.
More information about the Tutor