Bug 412401

Summary: Sometimes chained operations cause 'forgetting' of inserted constants or pasted numbers
Product: [Applications] kcalc Reporter: oleg.popkov
Component: generalAssignee: Evan Teran <evan.teran>
Status: RESOLVED FIXED    
Severity: normal CC: schiwed, Wolfram.Klaus
Priority: NOR    
Version First Reported In: 19.12   
Target Milestone: ---   
Platform: Manjaro   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description oleg.popkov 2019-09-27 18:13:10 UTC
SUMMARY
KCalc has a very annoying bug which manifests as forgetting entered constants and numbers pasted from the clipboard when they are followed by an operation other than '=' or '%' if the operation stack is not empty.

STEPS TO REPRODUCE
1. Click 'AC'
2. Click '+'
3. Either insert a "scientific" constant from the menu, or click a constant button, or paste a number from the clipboard
4. Click '+'

OBSERVED RESULT
The displayed number is '0'.

EXPECTED RESULT
The entered constant or pasted number must be displayed (i.e. number + 0). The correct behavior is observed if the number is entered manually at step 3.

SOFTWARE/OS VERSIONS
Windows: x
macOS: x
Linux/KDE Plasma: Arch Linux
(available in About System)
KDE Plasma Version: 5.16.5
KDE Frameworks Version: 5.62.0
Qt Version: 5.13.1
KCalc version: 19.08.1

ADDITIONAL INFORMATION
A possible solution is to call CalcEngine::setOnlyUpdateOperation(false) wherever applicable (e.g. core.setOnlyUpdateOperation(false) in KCalculator::slotConstantToDisplay()). However, I believe it would be better to implement a common method or slot for number insertion so that the same code would not get scattered over multiple places thereby adding to the technical debt. Also, this case should be covered by a set of tests.
Comment 1 cIdde 2020-03-31 10:27:14 UTC
Still present in 19.12.3 on Manjaro.
Comment 2 Wolfram Klaus 2020-05-17 09:01:44 UTC
STEPS TO REPRODUCE
1. CLICK 2*[constant]*3: Result is 5
2. CLICK 2*3*[constant]: Result is correct
3. CLICK [constant}*2*3: Result is correct
Comment 3 oleg.popkov 2020-05-18 16:13:28 UTC
As there has been no reaction from the developers for a long time, I submitted a naive solution to the Phabricator. Perhaps, it will be noticed there.

https://phabricator.kde.org/D29816
Comment 4 Maximilian Schiller 2020-06-07 08:00:54 UTC
Git commit cf39d29d737f7787350b04216c64a22a0b936af6 by Maximilian Schiller, on behalf of Oleg Popkov.
Committed on 07/06/2020 at 08:00.
Pushed by mschiller into branch 'master'.

Fix chained operations involving inserted numbers

In brief, KCalc uses a stack in `CalcEngine` for both numbers and operations in order to ensure the correct operation evaluation order (such that `2 + 2 * 2 = 6`) and support parentheses. Operations are pushed along with currently displayed numerical values. KCalc also checks whether it should push the operation onto the stack or just replace a pushed one with the new one without changing the associated number (so that `5 + - 3 = 2`), and for this it uses a flag changed by calling the `CalcEngine::setOnlyUpdateOperation` method. Obviously, this flag must be cleared after a number is entered by any method, but it only happens if a number is entered manually. As a result, inserting a number as a whole, whether as a constant or by pasting from the clipboard, results in replacing the preceding operation with the succeeding one without storing the number in question, unless the succeeding operation is `=` or `%`.

M  +19   -1    kcalc.cpp
M  +2    -0    kcalc.h

https://invent.kde.org/utilities/kcalc/commit/cf39d29d737f7787350b04216c64a22a0b936af6