I will first start with a scenario, I have to apply some adjustments to a particular value. These adjustments are either compound or non-compounded and they can either be added or subtracted to the value. They are executed in the order they are stored (they are stored in a database with a sequence number starting at 1).
The properties of these adjustments are stored in a database, so once I get them, I have access to the value, whether it is compounded or not and whether it should be added or subtracted from the base.
In all of the scenarios, I have 2 objects. The first object is to hold the adjustments which I read in from the database. The second object which I happen to call Divisor holds the collection of values depending on whether the adjustment is compounded, non-compounded, and should be subtracted or added. This will become more clear in my example.
loop through each adjustment if adjustment is compounded if compound adjustment is to be added assign 1 + (amount/100) to the divisor value else if compound adjustment is to be subtracted assign 1 - (amount/100) to the divisor value else if adjustment is non-compounded assign amount/100 to the divisor value
Once I have all the divisors, I loop through each divisor's value to construct the equation and apply it to my base value, so:
keep track of the calculated value in a variable called value initialized to 1. loop through each divisor if value is compound value = value * divisor value else if value is non-compound if divisor is to be added value = value + divisor value else if divisor is to be subtracted value = value - divisor value adjustedTotal = total * value
My first scenario involves a mixture of compounded and non-compounded percentages and adding and removing them from the base value:
Base Value: 100 Adjustments: +3% compounded -6% compounded +10% non-compounded -8% non-compounded
If I apply the above algorithm to the above adjustments and the above base value, I get 98.82 as my adjustedTotal. If I take 98.82 as total and apply the adjustments, but divide 98.82 by value, I get back to 100, so it appears that this works.
My second scenario is where I have all compound values and they are all added. If I use 100 as the base and apply a +10% and +7% compound adjustment, I get 117.7 which I believe is correct. Now if I take 117.7 and apply a -10% and -7% adjustment, I don't get back to 100, I get 140.62 if I divide the 117.7 by the value and 98.51 if I multiple 117.7 by the value.
My third scenario is where I have all non-compounded values and they are all added. If I use 100 as the base and apply a +10% and +7% non-compounded adjustment, I get 117 which I believe is correct. If I take 117 and apply a -10% and +7% non-compound adjustment, I get 97.11.
There are other scenarios which I won't bother with right here, but they include:
All Compound, Some Add, Some Subtract
All Non-Compound, Some Add, Some Subtract
In a nutshell, basically, I am looking for an algorithm where I can cover all the bases, but as you can see, it breaks in some scenarios and breaks in other scenarios. If I fix one scenario, I end up breaking another.
My setup is this, given a base value and a set of adjustments. If I apply these adjustments, I should get the correct value and if I reverse these adjustments, I should get back my original value.