0

I'm possibly writing a lot of terms incorrectly in this question since I'm kinda lost in how to look for this.

The problem I have is, given a monetary value that must be paid (a fee of some sort), and also given that this value comes from multiple sources, and I would like to correclty alocate them. How to correctly do the division. For instance, if I bought itens A, B and C, together they have a fee of $2.45. What I would do is:

fee of A alone = \$A/(\$A + \$B + \$C)*TotalFee

(and so on)

However, rounding the fees to two decimal places, and summing them at the end, sometimes gives a different value than the original fee.

I usually just adjust by hand, but I imagine there is a correct way to adjust that, possibly using the remaiders of division.

Thank you!

1 Answers1

0

This is surprisingly tricky to get right! Here is what I would do:

Call your three partial fees (expressed as a whole number of cents) $F_A,F_B,$ and $F_C$.

Suppose first that the sum of these is too small: $$F_A+F_B+F_C<245$$ First, get the partial fees $X_A,X_B,X_C$ to more decimal places, using standard floating-point arithmetic. Then calculate the three discrepancies $$D_A=X_A-F_A,D_B=X_B-F_B,D_C=X_C-F_C$$

Find the largest of these; say this is $D_B$ (if two or three are equally large, choose one at random). Now just increase $F_B$ by one cent.

This will usually be enough to make the total come out right. If not, just repeat the procedure; this will certainly be enough (for three items).

If $F_A+F_B+F_C$ is too large instead of too small, you should be able to work out how to modify this procedure accordingly.


Depending on your requirements, it might be more equitable to express those discrepancies as ratios: $$D_A=(X_A-F_A)/X_A,D_B=(X_B-F_B)/X_B,D_C=(X_C-F_C)/X_C$$ This is because, for instance, a 1-cent markup on a 10-cent item could be seen as more significant than a 2-cent markup on a 60-cent item. But this is your call.

TonyK
  • 64,559
  • Thank you. This is kind of what I was doing. I was wondering if there was some mathematically precise rule for that. – Diego Venturi Jan 09 '23 at 16:49
  • My description is mathematically precise, as far as I can see. I don't expect you will get a better answer. – TonyK Jan 09 '23 at 21:09