Splitting €10 three ways: the largest remainder method, and the tiebreak everyone forgets
Split €10 three ways and you get €3.33, €3.33, €3.33. That is €9.99. One cent has gone missing, and you now have to decide, in code, who pays it. It sounds like a rounding detail. It is not: it is the difference between an app whose numbers close and an app whose numbers almost close. I hit it while building an expense-sharing app, and the fix turned out to be a voting-theory algorithm from the…
Splitting a total of €10 among three people results in each person receiving €3.33, totaling €9.99. One cent is missing, and it is crucial to determine who should pay the extra cent. This issue is more than just a rounding problem; it can lead to significant discrepancies in larger expense-sharing scenarios. The solution lies in the largest remainder method, an algorithm from 1792 that ensures an exact division of indivisible units based on weights.
The method involves calculating each participant's exact share, assigning the floor of each share, and distributing the remaining units based on the largest fractional remainders. Implementing this method in integer cents guarantees that the total sum remains exact, preventing unnoticed errors that can accumulate over multiple expenses.
The tiebreak relies on participant IDs to maintain a deterministic and stable result across different runs and recalculations. This approach not only ensures that the total expense is accurately split but also guarantees that each participant's balance sums to exactly zero, providing a reliable test oracle for verifying the correctness of expense-sharing applications.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.


