GnuCash
Contact   Instructions
Bug 796949 - Incorrect conversion of 0,01 USD to EUR
Summary: Incorrect conversion of 0,01 USD to EUR
Status: RESOLVED FIXED
Alias: None
Product: GnuCash
Classification: Unclassified
Component: Currency and Commodity (show other bugs)
Version: 3.3
Hardware: PC Linux
: Normal normal
Target Milestone: ---
Assignee: core
QA Contact: core
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-18 20:46 EST by Yegor Derevenets
Modified: 2019-05-13 18:21 EDT (History)
5 users (show)

See Also:


Attachments
The book file to reproduce the problem (2.81 KB, application/gzip)
2018-11-18 20:46 EST, Yegor Derevenets
no flags Details
Screenshot demonstrating the problem (27.66 KB, image/png)
2018-11-18 20:47 EST, Yegor Derevenets
no flags Details
Fix round to handle numerators equal to 0 (611 bytes, patch)
2019-05-01 14:09 EDT, David
no flags Details
bugfix without tabs (632 bytes, patch)
2019-05-01 14:42 EDT, David
no flags Details
Fix division to return a negative remainder when dividend is negative (6.05 KB, patch)
2019-05-04 10:25 EDT, David
no flags Details

Description Yegor Derevenets 2018-11-18 20:46:09 EST
Created attachment 373059 [details]
The book file to reproduce the problem

On the Accounts page GnuCash shows the 0,01 USD in the Total column for Testtest account, however, the Total (EUR) column shows the incorrect value of 0,28 EUR for the same account. The correct value should be 0,01 EUR, and this value is actually shown in the Total (EUR) column for the Imbalance-USD account.

There are no conversion 1:28 conversion rates recorded in the currency prices database for the USD/EUR pair.

Strangely enough, the Total (EUR) amount is affected by the latest price of the only security in the book: when it is removed, the Total (EUR) value for the Testtest account turns into zero.

Also, changing the transaction sum in the Testtest account from 0,01 USD to any other value (like 0,02 USD or 1 USD) makes the problem go away: the ratio between the values in Total and Total (EUR) start to make sense again.

The gnucash book file to reproduce the problem is attached.

I am running GnuCash on Debian Testing:

gnucash:
  Installed: 1:3.3-2+b1
  Candidate: 1:3.3-2+b1
  Version table:
 *** 1:3.3-2+b1 500
        500 http://httpredir.debian.org/debian unstable/main amd64 Packages
        100 /var/lib/dpkg/status
     1:2.6.15-1 500
        500 http://httpredir.debian.org/debian stable/main amd64 Packages
Comment 1 Yegor Derevenets 2018-11-18 20:47:15 EST
Created attachment 373060 [details]
Screenshot demonstrating the problem
Comment 2 John Ralls 2018-11-21 20:57:39 EST
I'm not able to make a "Total (EUR)" column appear with the default currency set to EUR. I note that this isn't a well-formed book as the Root account has no currency and there are several transactions with a single split and 0 value and amount in the Imbalance-USD account. That's probably because you deleted some accounts without specifying that their transactions should be deleted. 

I do see, however, the bogus €0.28 Profit in the summary bar, so that gives me something to work with. Changing the amount of the sole transaction to $0.02 changes the profit to €0.02.

Either changing the date on the sole USD->EUR price to after the EL4B->EUR price or changing the EL4B price to before the USD one removes the bogus profit. That makes some sense because the Accounts page and the summary bar always work from the most recent price available.
Comment 3 Yegor Derevenets 2018-11-22 16:11:14 EST
Edit/Preferences/Report/Default Report Currency/Choose: EUR should make the "Total (EUR)" column available for you.
Comment 4 John Ralls 2018-11-22 18:46:50 EST
So it does, thanks. I don't agree that it should, report preferences should affect only reports, not the GUI.
Comment 5 John Ralls 2018-11-27 08:20:53 EST
As I sort-of expected, the problem was that .9 * -1 banker-rounds to 0, so the direct conversion to Euro is rejected and the indirect one is attempted; the indirect conversion then took any pair of prices that happened to share a commodity (any commodity!) without constraining the selection to requiring that the other two commodities in the pair matched the desired from and to ones.

I've fixed both problems for GnuCash 3.4.
Comment 6 Yegor Derevenets 2019-01-08 17:57:37 EST
Thank you for the fix, John!

However, after an upgrade to GnuCash 3.4 I see -0.01 EUR in Total (EUR) column for the problematic account. I guess, the correct value should be 0.01 EUR (without minus).

You can use the already attached bugreport2.gnucash to reproduce the problem.

I guess, the problem is not fixed completely, so, I reopen the bug.
Comment 7 John Ralls 2019-01-11 23:21:59 EST
Yup, I see it. Closer than €0.28, but still not right.
Comment 8 David 2019-05-01 14:09:09 EDT
Created attachment 373263 [details]
Fix round to handle numerators equal to 0

The problem was that the round function rounded up whenever the numerator was 0, and the remainder was greater than half the numerator, even when the remainder was negative.
Comment 9 Geert Janssens 2019-05-01 14:26:03 EDT
Why did you close this bug ? Maybe I missed something but I don't remember seeing your patch committed in our source code repository.
Comment 10 David 2019-05-01 14:32:54 EDT
I'm sorry, newbie's mistake.
Comment 11 David 2019-05-01 14:42:10 EDT
Created attachment 373264 [details]
bugfix without tabs

The same patch but only with spaces instead of tabs.
Comment 12 David 2019-05-01 19:17:16 EDT
Unfortunately, this fixes the issue but causes incorrect conversion in other places. I thought the integer divisions were C truncating divisions, however the divisions between GncInts follow the flooring division rule(remainder always positive), except when the result is between -1 and 0, in that case GncNumeric::prepare_conversion returns a negative remainder, so I would bet that the source of this bug is in the GncInt128::div function or somewhere related.
Comment 13 David 2019-05-04 10:25:57 EDT
Created attachment 373266 [details]
Fix division to return a negative remainder when dividend is negative

I believe I nailed it, now. The problem was that the division function was not flaging the remainder as a negative when the dividend was a negative, I also corrected the tests that were expecting this wrong behaviour. And the other bug was in the rounding functions that did not round correctly when the quotient was 0 but the remainder was negative.
Comment 14 David 2019-05-11 13:39:35 EDT
Link to pull request:
https://github.com/Gnucash/gnucash/pull/502
Comment 15 John Ralls 2019-05-13 18:21:13 EDT
David's pull request is merged and will be in GnuCash 3.6.

Note You need to log in before you can comment on or make changes to this bug.