in my situation I have a list with any amount of items. Each item has a percentage attached to it. All the percentages must add up to 1:
If L is the amount of items in the list, then here's an untouched L=3.
Item Percent
A 0.33...
B 0.33...
C 0.33...
Now I want to add something, say X to an item's percentage. Since they must all add up to 1, I have to subtract some number from all the other percentages. The problem is I don't know what that number is.
If L=2, the solution is simple, just subtract X from the item you're not adding to. Say I want to add 0.2 to A:
Item Percent
A 0.5 + 0.2
B 0.5 - 0.2
A 0.7
B 0.3
If L=3 or more, I was thinking of subtracting X/(L-1) from all the other items, but it fails beyond one calculation:
Adding 0.2 to A
Item Percent
A 0.25 + 0.2 = 0.45
B 0.25 - 0.2 / (4-1) = 0.1833...
C 0.25 - 0.2 / (4-1) = 0.1833...
D 0.25 - 0.2 / (4-1) = 0.1833...
Looks good, now adding 0.8 to D:
Item Percent
A 0.45 - 0.8 / (4-1)
B 0.1833... - 0.8 / (4-1)
C 0.1833... - 0.8 / (4-1)
D 0.1833... + 0.8
Here's what I end up with...
Item Percent
A 0.1833...
B -0.08336...
C -0.08336...
D 0.9833...
First of all, they don't add up to 1. Secondly, some of them are negative. Any simple solution I'm missing?
The problem is easy to see, since there is the possibility to subtract more percentage than a given item has.
If you want to solve this problem your way you have to check, whether there is enough percentage for the items to subtract. If the percentage is not big enough for an item then you set the percentage to 0 and you have to subtract the "lost" difference from another item (respectively from the other items). This should be easy to implement.. If the percentage of the other items is not important for you then you can go a more simple way and give the missing percentage to 1 in similar parts to them (and ignore their given percent-values)...
If the relative relations between the values of the other items should hold, then your solution is more difficult since you have to solve a linear equation system.