chip;11028481 wrote:I changed 1.01 to 1.02 now the calculation is right. I think.
How many test cases did you try to determine this? (Or did you just play with the numbers until this one single example gave you the expected value and assumed the calculation was valid for all values?)
Can you explain in words what it is you're trying to calculate? This:
is adding 2% to the second "100" before it is used in the summation. That is not the same as adding 1% to the sum of the first two numbers:
(100 + 100) * 1.01 + 100 + 100
= 200 * 1.01 + 100 + 100
= 202 + 100 + 100
= 402
EDIT: To illustrate the difference, replace the identical "100" values with "100", "200", "300", and "400" and try the two different expressions again:
100 + 200 * 1.02 + 300 + 400
= 100 + 204 + 300 + 400
= 10,004
(100 + 200) * 1.01 + 300 + 400
= 300 * 1.01 + 300 + 400
= 303 + 300 + 400
= 10,003