Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zero-amount transfers and other rounding issues #164

Merged
merged 3 commits into from
Jan 16, 2017

Conversation

zorun
Copy link
Collaborator

@zorun zorun commented Jan 2, 2017

These are workarounds for rounding issues.

@@ -61,9 +61,9 @@ def get_transactions_to_settle_bill(self):
credits, debts, transactions = [],[],[]
# Create lists of credits and debts
for person in self.members:
if balance[person.id] > 0:
if round(balance[person.id], 2) > 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'll be better to do the rounding in the balance() function instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I was also thinking, but I decided against it since balance() seems to be used in a lot of other places. Some calling functions may need the full floating-point precision to work.

It's a bit like unicode vs. bytestrings: always use unicode internally, and only decode/encode to UTF-8 bytestrings when handling inputs/ouputs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, looks good to me.

@JocelynDelalande
Copy link
Contributor

Thanks a lot, welcome here :-)

👍 (tested & reviewed), although I don't know how to trigger the problematic cases, so my test is barely "it does not breaks the existing".

Should be easy to provide a test, if you have time, at least for 72f177a ?

Baptiste Jonglez added 3 commits January 3, 2017 21:37
This workarounds a rounding issue caused by the (incorrect) usage of
floats for bill amounts.

This fixes spiral-project#138
A user with a "0.00" balance would have either a "+0.00" in green or a
"-0.00" in red, depending on the exact value of the floating-point value.

Fix this by simply rounding to 2 digits before comparing to zero.
@zorun zorun force-pushed the fix_zero_transfers branch from 3c4c6e4 to d6cf890 Compare January 3, 2017 20:37
@zorun
Copy link
Collaborator Author

zorun commented Jan 3, 2017

@JocelynDelalande good idea :)

I have added a test for the zero-transfer issue, this is the output before applying b507a5a :

======================================================================
FAIL: test_settle_zero (__main__.BudgetTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests.py", line 622, in test_settle_zero
    msg='%f is equal to zero after rounding' % t['amount'])
AssertionError: 0.003333 is equal to zero after rounding

@almet almet merged commit dc75a72 into spiral-project:master Jan 16, 2017
@almet
Copy link
Member

almet commented Jan 16, 2017

Thanks :)

Jojo144 pushed a commit to Jojo144/ihatemoney that referenced this pull request Mar 21, 2020
Fix zero-amount transfers and other rounding issues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants