>BTW, in that formula, I entered by mistake the amount (500.00$) instead of the tax amount (74.88$) and it ended up in an infinite loop. Do you know what would need to be adjusted in the formula to avoid an infinite loop in case a bad tax amount would be provided?
I'ts not a loop, it just ends without finding a solution because a total tax of $500.00 does not exist
What happens is that fedTax + provTax is 499.99 at one moment and the next iteration it is 500.01
So I have added an extra if: fedTax + provTax > Totaltax, then adjust provTax to be totalTax - fedtax so that the sum of both of them is 500.00
totalTax = 500.00
f = .05
p = .095
base = round(totalTax / (f*p + f + p), 2)
for i = base - .50 to base + 0.50 step .01
fedTax = round(i * f, 2)
provTax = round((i + fedTax) * p, 2)
if ( fedTax + provTax == totalTax )
? 'Base ' , i
? 'fedtax', fedTax
? 'provTax ', provTax
exit
endif
if( fedTax + provTax > totalTax )
provTax = totalTax - fedtax
? 'Base ' , i
? 'fedtax', fedTax
? 'provTax ', provTax
exit
endif
endfor
Gregory