oss-apps / split-pro

Open source alternative to Splitwise
https://splitpro.app
GNU Affero General Public License v3.0
127 stars 10 forks source link

Numerical stability #51

Open kouyk opened 2 months ago

kouyk commented 2 months ago

It seems like currencies are handled as javascript floats. This is probably going to cause quite a lot of numerical instability issues. For example, I was trying to add a transaction that is $11.10 in total, then by using the exact split option, entering $5.20 for the first person results in $5.90 remaining, the calculated value is inexact $5.8999... This made it difficult to pass the verification. While there are workarounds such as splitting by shares, it doesn't solve the fundamental issues with using floats with currencies. Larger values will cause even more issues. Maybe this aspect could use some tests for such edge cases and ensure that the sums are correct.

KMKoushik commented 2 months ago

Yes, rounding off to 2 digits will actually fix this issue easily, As I am storing everything as whole number in DB. Good catch will work on it. Thanks

kouyk commented 1 month ago

As I used the application more frequently, I'm starting to find edge cases where storing as whole number in DB does not seem to be able to solve the issue since the split is determined in the frontend.

The set up consists of two users, Alice (current user) and Bob in a new group.

Steps:

  1. Splitting $32.61 equally, paid by Alice.
  2. Observe in the split dialogue that both parties will be liable for $16.30 which clearly does not sum up to the actual total
  3. After the transaction has been added, it can be seen that Alice lent $16.30, while for the transaction just added, it shows $16.31 is being lent.
  4. Tapping into the transaction will reveal that Alice and Bob each owe $16.30.
  5. If a similar transaction is added, this time paid by Bob, Alice will consistently own $16.30 from the point of view of the current user.

Splitwise is observed to randomly assign the final cent in this case, which is updated each time the transaction is updated as well.