I'm trying to solve the following real-life problem you might have encountered yourselves:
You had dinner with some friends and you all agreed to split the bill evenly. Except that when the bill finally arrives, you find out not everyone has enough cash on them (if any, cheap bastards).
So, some of you pays more than others... Afterwards you come home and try to decide "who owes who what amount?".
This, I'm trying to solve algorithmically & fair :)
it seems so easy at first, but I'm getting stuck with rounding and what not, I feel like a total loser ;)
Any ideas on how to tackle this?
EDIT: Some python code to show my confusion
>>> amounts_paid = [100, 25, 30]
>>> total = sum(amounts_paid)
>>> correct_amount = total / float(len(amounts_paid))
>>> correct_amount
51.666666666666664
>>> diffs = [amnt-correct_amount for amnt in amounts_paid]
>>> diffs
[48.333333333333336, -26.666666666666664, -21.666666666666664]
>>> sum(diffs)
7.1054273576010019e-015
Theoratically, the sum of the differences should be zero, right?
for another example it works :)
>>> amounts_paid = [100, 50, 150]
>>> total = sum(amounts_paid)
>>> correct_amount = total / float(len(amounts_paid))
>>> correct_amount
100.0
>>> diffs = [amnt-correct_amount for amnt in amounts_paid]
>>> diffs
[0.0, -50.0, 50.0]
>>> sum(diffs)
0.0