Bug 402750 - Rounding problems with stock split factor cause crashes and errors
Summary: Rounding problems with stock split factor cause crashes and errors
Status: RESOLVED FIXED
Alias: None
Product: kmymoney
Classification: Applications
Component: general (show other bugs)
Version: 5.0.1
Platform: Ubuntu Linux
: NOR major
Target Milestone: ---
Assignee: KMyMoney Devel Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-01-01 10:27 UTC by Mark Thomas
Modified: 2019-01-01 16:33 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In: 5.0.3


Attachments
backtrace of divide by zero (8.48 KB, text/plain)
2019-01-01 10:27 UTC, Mark Thomas
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Thomas 2019-01-01 10:27:10 UTC
Created attachment 117222 [details]
backtrace of divide by zero

SUMMARY

kMyMoney incorrectly rounds the factor for a stock "Split Shares" activity instead of the resulting balance causing divide-by-zero or incorrect calculations.


STEPS TO REPRODUCE
1. Under Investments create a stock with the fraction set to 1 (ie: 1/1, whole stocks).
2. In the ledger create a new "Buy Shares" activity for the stock with any quantity and price.
3. In the ledger create a new "Split Shares" activity for the stock and set the ratio to less than 0.5 (eg: 0.166667 for a 1-for-6 share consolidation)

OBSERVED RESULT

On entering the split shares activity kMyMoney will immediately crash with a divide-by-zero exception, see attached stack trace.

EXPECTED RESULT

A ratio less than 1 should create a share consolidation, decreasing the balance of shares by the given factor.

Alternatively in step 3 enter the ratio of 1.5 for a 3-for-2 stock split and observe that kMyMoney rounds the factor up to 2 and incorrectly doubles the balance.

The rounding for the share fraction should be done later on the new balance of shares after the split factor is applied and not on the factor itself.


SOFTWARE/OS VERSIONS

kmymoney 5.0.1-2 Kubuntu package and 5.0.2 compiled from git.

Linux/KDE Plasma: Kubuntu 18.04 LTS 
KDE Plasma Version: 5.12.7
KDE Frameworks Version: 5.44.0
Qt Version: 5.9.5

ADDITIONAL INFORMATION

I have a stock that has had a 1-for-6 share consolidation so needed to reduce the quantity down by the ratio of one for every 6 currently held.

Entering this as a "Split Shares" activity with a factor of 0.16667 (ie: 1 divided by 6) causes a divide by zero floating point exception.  

The critical point is that the stock is defined with  a 1/1 fraction (ie: whole stocks) and that this causes the split factor in the activity to be rounded down to zero.

This rounding  is also an issue with normal splits of whole stocks, for example a 3:2 split entered as factor 1.5 as per the manual is rounded up to 2.

I suspect that other unusual splits may have rounding problems with other fractions.

The problem code appears to be in in kmymoney/dialogs/investtransactioneditor.cpp where InvestTransactionEditor::createTransaction() calls:

s0.setShares(MyMoneyMoney(s0.shares().convertDenominator(securityFraction, roundingMethod)));

with securityFraction is 1 but the value in s0 is the factor, not the total quantity of shares, and needs to keep the precision it was entered with.

The rounding would need to be moved to the split calculation on the share balance in KGlobalLedgerViewPrivate::loadView() in kmymoney/views/kgloballedgerview_p.h
Comment 1 Thomas Baumgart 2019-01-01 15:28:17 UTC
Since for stock splits only the factor of the split is stored, we must not round it at all.
Comment 2 Thomas Baumgart 2019-01-01 16:33:08 UTC
Git commit f800f7666ee31f64f8d17bbf4c51585e006c4922 by Thomas Baumgart.
Committed on 01/01/2019 at 16:34.
Pushed by tbaumgart into branch '5.0'.

Don't change stock split factor by rounding

Rounding of the stock split factor may cause the factor to become zero
which will later result in a division by zero. The factor must not be
rounded to solve the problem.
FIXED-IN: 5.0.3

M  +1    -9    kmymoney/dialogs/investactivities.cpp
M  +6    -3    kmymoney/dialogs/investtransactioneditor.cpp
M  +6    -3    kmymoney/mymoney/mymoneyfile.cpp

https://commits.kde.org/kmymoney/f800f7666ee31f64f8d17bbf4c51585e006c4922