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

ledger needs support for rounding (BZ#992) #992

Open
tbm opened this issue Sep 24, 2013 · 9 comments
Open

ledger needs support for rounding (BZ#992) #992

tbm opened this issue Sep 24, 2013 · 9 comments
Labels
enhancement New feature or request P1 Highest priority

Comments

@tbm
Copy link
Contributor

tbm commented Sep 24, 2013

Note: the issue was created automatically with bugzilla2github

Bugzilla bug ID: BZ#992
From: Bradley M. Kuhn (@bkuhn)
CC: @aleksejrs, @jwiegley, @tbm, @thdox, @tripun

@tbm
Copy link
Contributor Author

tbm commented Sep 24, 2013

Comment author: Bradley M. Kuhn (@bkuhn)

This fails:
2013-11-18 Test
Expenses -625.00 EUR {=$1.3262}
Expenses -896.15 EUR {=$1.3348}
Accrued $828.88
Accrued $1,196.18

with:
Unbalanced remainder is:
$2,025.06
-625.00 EUR {=$1.3262}
-896.15 EUR {=$1.3348}
Amount to balance against:
$2,025.06

This works:

2013-11-18 Test
Expenses -625.00 EUR {=$1.3262} @ $1.3262
Expenses -896.15 EUR {=$1.3348} @ $1.3348
Accrued $828.88
Accrued $1,196.18

as does this:

2013-11-18 test2
Expenses $-34.60
Expenses -775.51 CAD {=$0.9524} @ $0.9524
Accrued $773.20

But merged together:

2013-11-18 Test
Expenses -625.00 EUR {=$1.3262} @ $1.3262
Expenses -896.15 EUR {=$1.3348} @ $1.3348
Accrued $828.88
Accrued $1,196.18
Expenses $-34.60
Expenses -775.51 CAD {=$0.9524} @ $0.9524
Accrued $773.20

I get:
Unbalanced remainder is:
$0.01
Amount to balance against:
$2,798.26

What gives?

@tbm
Copy link
Contributor Author

tbm commented Mar 20, 2014

Comment author: Tripun (@tripun)

Created attachment 96
test input data

Round off occur when the amount of commodity in a transaction has less precision than the calculation involved.
Evaluation is as below:
"
This fails:
2013-11-18 Test
Expenses -625.00 EUR {=$1.3262}
Expenses -896.15 EUR {=$1.3348}
Accrued $828.88
Accrued $1,196.18

with:
Unbalanced remainder is:
$2,025.06
-625.00 EUR {=$1.3262}
-896.15 EUR {=$1.3348}
Amount to balance against:
$2,025.06
"
Expenses are non-zero but cannot be converted to $ which is the unit of display and hence unbalanced amount is what is available in dollars.

==============
"
2013-11-18 Test
Expenses -625.00 EUR {=$1.3262} @ $1.3262
Expenses -896.15 EUR {=$1.3348} @ $1.3348
Accrued $828.88
Accrued $1,196.18
"
After conversion balance is $0.003(approx), the reporting precision is 2 digits since the amounts(quantity) involved have maximum of 2 digit precision. After round off the balance is zero and hence there is no unbalanced amount and the test passes.

=====================
"

2013-11-18 test2
Expenses $-34.60
Expenses -775.51 CAD {=$0.9524} @ $0.9524
Accrued $773.20

"
Same explanation as above. round off leads to zero unbalanced amount.

=====================================
"
2013-11-18 Test
Expenses -625.00 EUR {=$1.3262} @ $1.3262
Expenses -896.15 EUR {=$1.3348} @ $1.3348
Accrued $828.88
Accrued $1,196.18
Expenses $-34.60
Expenses -775.51 CAD {=$0.9524} @ $0.9524
Accrued $773.20
"

Attached file: roundoff.dat (text/plain, 791 bytes)
Description: test input data

@tbm
Copy link
Contributor Author

tbm commented Mar 20, 2014

Comment author: Tripun (@tripun)

"
2013-11-18 Test
Expenses -625.00 EUR {=$1.3262} @ $1.3262
Expenses -896.15 EUR {=$1.3348} @ $1.3348
Accrued $828.88
Accrued $1,196.18
Expenses $-34.60
Expenses -775.51 CAD {=$0.9524} @ $0.9524
Accrued $773.20
"

After conversion balance is $0.008(approx), the reporting precision is 2 digits since the amounts(quantity) involved have maximum of 2 digit precision. After round off the balance is $0.01 and hence there is unbalanced amount and the test fails.

@tbm
Copy link
Contributor Author

tbm commented Apr 17, 2014

Comment author: John Wiegley (@jwiegley)

Bradley, you have a point that if the @ version works, the {=} version should
work equally well. I'll make a separate bug for that.

@tbm
Copy link
Contributor Author

tbm commented May 27, 2014

Comment author: Martin Michlmayr (@tbm)

(In reply to comment #3)

Bradley, you have a point that if the @ version works, the {=} version
should work equally well. I'll make a separate bug for that.

I believe it's already covered by bug BZ#630.

@tbm
Copy link
Contributor Author

tbm commented May 27, 2014

Comment author: Martin Michlmayr (@tbm)

I'm changing this to an enhancement request: ledger needs support for rounding. As Tripun pointed out, there is no bug as such. It's just that ledger keeps a lot of precision internally and this can lead to errors.

In most cases, we want to round to 2 digits. There should be a way for ledger to do such rounding.

@tbm
Copy link
Contributor Author

tbm commented May 27, 2014

Comment author: Martin Michlmayr (@tbm)

(In reply to comment #2)

"
2013-11-18 Test
Expenses -625.00 EUR {=$1.3262} @ $1.3262
Expenses -896.15 EUR {=$1.3348} @ $1.3348
Accrued $828.88
Accrued $1,196.18
Expenses $-34.60
Expenses -775.51 CAD {=$0.9524} @ $0.9524
Accrued $773.20
"

After conversion balance is $0.008(approx), the reporting precision is 2
digits since the amounts(quantity) involved have maximum of 2 digit
precision. After round off the balance is $0.01 and hence there is
unbalanced amount and the test fails.

Yes, and you can see this if you put the following in the file:

D $1000.000

Unbalanced remainder is:
$0.008
Amount to balance against:
$2,798.260

@tbm
Copy link
Contributor Author

tbm commented May 27, 2014

Comment author: Martin Michlmayr (@tbm)

So here's an interesting example which shows that you can get different results to what you'd expect because of ledger keeping so much precision.

Using a lot of precision using figures like @ $1.3762 gives us:

2014-01-01 * Expense in EUR
Accounts Payable -€154.90 {=$1.3762} @ $1.3762
Expenses:foo

2014-02-01 * Person paid 4338.00 CZK, not 154.90 EUR
Accounts Payable €154.90 {=$1.3762} @ $1.3762
Accounts Payable -4338.00 CZK {=$0.05026} @ $0.05026
Expenses:Currency $4.85

Note that Expenses:Currency is $4.85.

But now let's use amounts rounded to 2 digits:

2014-01-01 * Expense in EUR
Accounts Payable -€154.90 {{=$213.17}} @ $1.3762
Expenses:foo

2014-02-01 * Person paid 4338.00 CZK, not 154.90 EUR
Accounts Payable €154.90 {{=$213.17}} @ @ $213.17
Accounts Payable -4338.00 CZK {{=$218.03}} @ @ $218.03 ; $0.05026
Expenses:Currency $4.86

Now Expenses:Currency is $4.86.

Why the difference?

With full precision:

154.90 * 1.3762 = 213.17338
-4338.00 * 0.05026 = -218.02788
213.17338 - 218.02788 = -4.8545

4.8545, which is rounded to 4.85 at display precision 2.

On the other hand, using the amounts that were rounded to 2 digits fo precision:

213.17 - 218.03 = -4.86

Obviously, both solutions are correct.

But there should be a way to specify a rounding precision, so you can have the first example (i.e. exchange amounts specified as @ $1.3762) and still get an Expenses:Currency of $4.86.

@tbm
Copy link
Contributor Author

tbm commented May 30, 2014

Comment author: Martin Michlmayr (@tbm)

Another request for rounding is discussed in #781 (with a good test case).

@tbm tbm added P1 Highest priority enhancement New feature or request labels May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request P1 Highest priority
Projects
None yet
Development

No branches or pull requests

1 participant