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

1.32+: inaccurate value reports caused by low precision P amounts #2254

Open
akanshaG42 opened this issue Oct 5, 2024 · 6 comments
Open
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. journal The journal file format, and its features. regression A backwards step, indicating a weakness in our QA. We don't like these. valuation

Comments

@akanshaG42
Copy link

Hi, seems like transactions are not balances with multiple commodities, potentially due to rounding issue.

b.journal

D R1,23,45,678.0 ; defualt currency of R

P 2023-09-15 USD 83.1 ; USD to R on 09/15

2023-09-15
    assets:equity:acme    66 acme @ 10.2 USD
    income:acme:rsu

Now if I want to see the balance in R, as per docs I will have to pass the and --infer-market-prices and --value=then,R so that purchase/cost price is used.

$ ~/Downloads/hledger-linux-x64/hledger -f b.journal bal   --infer-market-prices --value=then,R

           R55,941.6  assets:equity:acme
          R-55,942.9  income:salary:acme
--------------------
               R-1.3

The -1.3 seems unexpected. The posting are completely balanced if I use round numbers.

b.journal

D R1,23,45,678.0 ; defualt currency of R

P 2023-09-15 USD 83 ; USD to R on 09/15

2023-09-15
    assets:equity:acme    66 acme @ 10 USD
    income:salary:acme
$ ~/Downloads/hledger-linux-x64/hledger -f b.journal bal  --infer-market-prices --value=then,R

           R54,780.0  assets:equity:acme
          R-54,780.0  income:salary:acme
--------------------
                   0

I have gone through the manual sections of Cost and Value reporting as well as the Track Investments cookbook multiple time and seems like the command line flags are correct.

Though I have been using hledger for many years, this is the first time I have to deal with foreign stocks and equity, so it is completely possible that I am doing something wrong.

Would appreciate any help.

Thanks!

Version Info

$ ~/Downloads/hledger-linux-x64/hledger --version

hledger 1.40, linux-x86_64
@akanshaG42 akanshaG42 changed the title Unbalanced Unbalanced accounts with multiple commodity posting Oct 5, 2024
@simonmichael simonmichael added journal The journal file format, and its features. valuation labels Oct 6, 2024
@simonmichael
Copy link
Owner

simonmichael commented Oct 6, 2024

Hi @akanshaG42, thanks for the report. I must admit I'm not seeing an explanation just yet!

Here's a simpler example:

P 2023-09-15 USD R100

2023-09-15
    assets    50 acme @ 10 USD
    income

The postings will have the same cost (by definition), and if valued with the same market prices, should also have the same value:

$ hledger print -x
2023-09-15
    assets    50 acme @ 10 USD
    income            -500 USD

$ hledger print -x --infer-market-prices -X R
2023-09-15
    assets          R50000
    income         R-50000

I can change the price and/or cost to any integer and the posting values still match.
But if I give both price and cost some non-zero decimal digits,

P 2023-09-15 USD R100.5

2023-09-15
    assets    50 acme @ 10.5 USD
    income

the postings are valued differently:

$ hledger print -x
2023-09-15
    assets    50 acme @ 10.5 USD
    income            -525.0 USD

$ hledger print -x --infer-market-prices -X R
2023-09-15
    assets          R52760
    income       R-52762.5

I expected both postings to be valued at R52762.5.

@simonmichael
Copy link
Owner

simonmichael commented Oct 6, 2024

A simpler case:

P 2000-01-01 USD R 100.5

2000-01-01
    (a)    50 ACME @ 10.5 USD

And it turns out to be a regression, since hledger 1.32:

$ hledger-1.31 print --infer-market-prices -X R
2000-01-01
    (a)       R 52762.5

$ hledger-1.32 print --infer-market-prices -X R
2000-01-01
    (a)         R 52760

During the chain of (two) conversions, an intermediate value is inappropriately rounded to the precision of the P directive's amount (one decimal place), causing inaccuracy.

You can work around it for now by making sure P amounts have enough decimal digits. Eg for this example, changing it to P 2000-01-01 USD R 100.50 makes the problem go away.

Thank you @akanshaG42! Please claim your $50 https://hledger.org/REGRESSIONS.html#regression-bounty when convenient.

@simonmichael simonmichael added A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. regression A backwards step, indicating a weakness in our QA. We don't like these. labels Oct 6, 2024
@simonmichael simonmichael changed the title Unbalanced accounts with multiple commodity posting 1.32+: Unbalanced accounts with multiple commodity posting Oct 6, 2024
@simonmichael simonmichael changed the title 1.32+: Unbalanced accounts with multiple commodity posting 1.32+: low precision P amounts can cause inaccurate value reports Oct 6, 2024
@simonmichael simonmichael changed the title 1.32+: low precision P amounts can cause inaccurate value reports 1.32+: inaccurate value reports caused by low precision P amounts Oct 6, 2024
simonmichael added a commit that referenced this issue Oct 6, 2024
…#2254]

Valuation in another commoditay could sometimes be inaccurate if the P
price amounts did not have enough decimal places.
@simonmichael
Copy link
Owner

This is fixed in master.
(Linux x64 binary here if it helps.)

@akanshaG42
Copy link
Author

Thank you for an incredibly fast turn around here @simonmichael. I tried the CI build with my actual ledger and it is balanced now. Thanks again!

@simonmichael
Copy link
Owner

Hi @akanshaG42, I encourage you to claim your $50 regression finder's bounty within the next 7 days! If not, it will be recorded as a project donation.
https://hledger.org/REGRESSIONS.html#regression-bounty

simonmichael added a commit that referenced this issue Oct 9, 2024
…#2254]

Valuation in another commoditay could sometimes be inaccurate if the P
price amounts did not have enough decimal places.
@akanshaG42
Copy link
Author

Hi @akanshaG42, I encourage you to claim your $50 regression finder's bounty within the next 7 days! If not, it will be recorded as a project donation. https://hledger.org/REGRESSIONS.html#regression-bounty

That's alright @simonmichael. Please consider it as a project donation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. journal The journal file format, and its features. regression A backwards step, indicating a weakness in our QA. We don't like these. valuation
Projects
None yet
Development

No branches or pull requests

2 participants