[Tutor] very odd math problem
Dave Angel
davea at ieee.org
Fri Mar 11 16:05:56 CET 2011
On 01/-10/-28163 02:59 PM, Steven D'Aprano wrote:
> Alan Gauld wrote:
>
>> 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?
>
> The context was generating a list of values, not just the final value.
> But the result can be generalized to other situations. Consider some
> function that calculates a result x by an iterative process. You don't
> care about the intermediate results, but you do have to step through
> them on the way to the final result:
>
> x = initial_value
> while condition:
> x += increment(x)
>
>
> When possible, it is better to re-write the formula to avoid repeatedly
> adding an increment to x. The problem is, if each addition has potential
> error of dx, then N additions have potential error N*dx -- you can't
> assume that errors will always cancel. If N is large, so is the expected
> error.
>
> <snip>
>
I like to think of this as the social security problem, as that was the
context in which I first saw it. When figuring social security
withholding tax (USA), the employer is not allowed to just figure it on
the basis of the current wage amount. If he did, the amount deducted
would be rounded/truncated to the penny, and those partial pennies could
add up to an enormous amount. Instead he figures the total soc.sec. tax
on the pay for the year, and from that subtracts the amount withheld in
all previous checks. So the total is always within a fractional penny
of the correct amount.
Any time you can't store the intermediate amount exactly, you need to
decide how/if to eliminate accumulating residuals.
DaveA
More information about the Tutor
mailing list