Bug 167986

Summary: calculator runner of krunner outputs wrong results
Product: [Plasma] krunner Reporter: Antonis <linuzonix>
Component: generalAssignee: Plasma Bugs List <plasma-bugs>
Status: RESOLVED FIXED    
Severity: normal CC: alex-kostjukov, anselmolsm, bennyahahn, bleamz, catcatmu, chagam, charlyghislain, clcevboxvjeo, dariopnc, echidnaman, erpo41, esigra, guido-kdebugs, ivo, jagarni1983, jakobloewen, kde-bugs, mail, mangoo, metalyr, muziofg, nate, poletti.marco, raphael, sts, vivek.ap, wilderkde, zahl
Priority: HI    
Version: unspecified   
Target Milestone: ---   
Platform: Unlisted Binaries   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Antonis 2008-08-01 13:58:57 UTC
Version:            (using KDE 4.1.0)
Installed from:    Unspecified Linux
OS:                Linux

The calculator runner of the krunner app yields wrong results.
For example if you enter in krunner =8,2+1,1 instead of 9,3 it outputs 9.299999999999999
Comment 1 George Kiagiadakis 2008-08-02 11:14:42 UTC
I can confirm this, but actually it's not a wrong result as 0.99999... = 1  (http://en.wikipedia.org/wiki/0.999...#Proofs). It's just not the result that humans would expect. Maybe this could be solved by using higher precision data types for the calculation...
Comment 2 A. Spehr 2008-08-02 11:25:38 UTC
=8.2+1.1 for non-comma countries. This isn't IEEE, ergo, bug.
Comment 3 A. Spehr 2008-08-02 11:34:01 UTC
Actually. I lie.
Comment 4 Jaime Torres 2008-08-02 11:56:41 UTC
it also says that 1*10^-15 is 1e-15 and 1e-15 is 1.

0.99999999999 is not 0.9999... (infinite nines).
Comment 5 Karthik Periagaram 2008-08-04 07:18:42 UTC
It appears krunner is unable to understand exponential notation. Entering "=1/4e-3" produces 64 instead of 250. I entered 1/(4*10^-3) and got the correct answer. I had to put brackets around the denominator as it doesn't seem to respect the BODMAS/BEDMAS/PEDMAS/whatever rule (http://en.wikipedia.org/wiki/BODMAS), either. There definitely is something wicked going on here...
Comment 6 Dmitry Suzdalev 2008-10-22 21:30:18 UTC
*** Bug 161555 has been marked as a duplicate of this bug. ***
Comment 7 Javier G. 2008-12-06 15:25:49 UTC
I confim that the krunner outputs are wrong:
= 595056260442243700 + 1
  595056260442243700  (the same original value, "+1" didn't work)

I using the SVN:
kde4-config --version
Qt: 4.4.3
KDE: 4.1.82 (KDE 4.1.82 (KDE 4.2 >= 20081204))
kde4-config: 1.0
Comment 8 FiNeX 2008-12-12 18:14:44 UTC
*** Bug 177464 has been marked as a duplicate of this bug. ***
Comment 9 Raphael Borg Ellul Vincenti 2008-12-17 09:42:03 UTC
When you do "=789.28-616.47" the result is "= 172.80999999999995" (WRONG). However when you do "=789.28-616.4" the result is "= 172.88" (CORRECT).
Comment 10 BLeAm 2009-01-21 03:39:00 UTC
It also happens with =100*0.07 it returns 7.000000000000001 as a result which is wrong, pretty weird.
Comment 11 Sasha Unspecified 2009-02-24 14:48:42 UTC
In KDE 4.2 krunner says that =4^2-2*4*9/2+(9/2)^2 is 2
Comment 12 Dominik Tritscher 2009-04-03 23:10:38 UTC
*** Bug 188617 has been marked as a duplicate of this bug. ***
Comment 13 Catcatmu 2009-04-24 06:25:22 UTC
I think the problem comes because it doesn't round up the last digit.
Take =1/6 for example, It gives 0.16666(...) instead of 1.66(...)7
Comment 14 Dario Andres 2009-05-06 20:39:41 UTC
*** Bug 191834 has been marked as a duplicate of this bug. ***
Comment 15 charly ghislain 2009-05-28 16:02:34 UTC
=100*0.009 shows 0.900000000001 but =100*0.09 shows 9. =10*0.09 shows 0.90000000001 as well. Weird.
Comment 16 Sasha Unspecified 2009-05-28 22:56:05 UTC
IMHO, it's not so bad. The gotten result is very near to the correct one.

Much wore when calculation result has nothing similar with correct one.
Like in example I shown above: "krunner says that =4^2-2*4*9/2+(9/2)^2 is 2" (while it must be 0.25 in reality).
Comment 17 Unknown 2009-05-28 23:01:56 UTC
Like I pointed out in my bug report (redirected here from https://bugs.kde.org/show_bug.cgi?id=177464), expressions with multiple exponents compute to whatever you get when you drop everything before the last exponent. For example:

=2^2*2 is 4*4, and correctly computes to 16. On the other hand, =2^2^2*2 computes to 4. It's just the 2*2 after the last ^.
Comment 18 Ivo Anjo 2009-05-28 23:05:16 UTC
Maybe krunner could use kcalc/knumber for calculations? Or maybe kalgebra?
Speedcrunch?
Comment 19 Dario Andres 2009-06-15 03:49:18 UTC
*** Bug 196506 has been marked as a duplicate of this bug. ***
Comment 20 clcevboxvjeo 2009-06-15 13:26:58 UTC
6.6+0.8= 7.39999995 :)
KDE 4.2.4
Comment 21 esigra 2009-06-15 23:10:53 UTC
> 6.6+0.8= 7.39999995 :)
> KDE 4.2.4

Woahh! Better stay with 3.5 until that is fixed.
Comment 22 Dario Panico 2009-06-24 14:08:30 UTC
bug confirmed with recent svn snapshot

any work on this?

I think it's quite ridiculous for a mature DE having a calc that isn't actually able to give the right answer
Comment 23 Benny 2009-07-09 18:29:46 UTC
Krunner also has problems calculating Sin(2*PI)=-2.4492935982947064e-16.
But cos(2*PI)=1 gives what i would expect.

Sytsem: KDE 4.2.4 from Debian unstable on Linux amd64 2.6.30-1-amd64 with Qt 4.5.1.
Comment 24 Jonathan Thomas 2009-07-26 22:25:39 UTC
Looks like the calculator runner is using the QScriptEngine::evaluate function to do its calculations (http://doc.trolltech.com/4.5/qscriptengine.html#evaluate)
So either this is a Qt bug or the calculator runner is abusing the function.
Comment 25 Ivo Anjo 2009-07-27 11:15:57 UTC
(In reply to comment #24)
> Looks like the calculator runner is using the QScriptEngine::evaluate function
> to do its calculations
> (http://doc.trolltech.com/4.5/qscriptengine.html#evaluate)
> So either this is a Qt bug or the calculator runner is abusing the function.

As I had said in my original bug report that got marked as a dupe for this one:
"Users expect a calculator to display precise values, for example 1.3+8.15 as 9.45 (as it does in kcalc) and not 9.450000000000001 because of floating point rounding errors."

You are right: QScriptEngine is being abused to implement this calculator, which leads to all these strange results.
Comment 26 FiNeX 2009-08-16 18:57:28 UTC
*** Bug 204041 has been marked as a duplicate of this bug. ***
Comment 27 Dario Andres 2009-08-19 04:22:03 UTC
*** Bug 204354 has been marked as a duplicate of this bug. ***
Comment 28 erpo41 2009-08-19 04:32:22 UTC
If the calculator can't produce correct results then it needs to be removed.
Comment 29 spamfang1199 2009-10-03 17:46:21 UTC
System: KDE 4.3.1 from Debian testing.

in krunner: when entering =19.99-3.99=15.999999999999998 instead of 16.

But when entering =19.99-1.99=18.

Question: isn't it possible to somehow use Kcalc for Krunner calculation?
Comment 30 Vivek 2009-11-23 03:11:00 UTC
I've run into the same annoying rounding error with krunner in KDE 4.3.3.  For me, they key issue is that entering "8.2+1.1<9.3=" returns "true" in krunner, whereas the answer is clearly false.  

A piece of advice: some statistical programs such as Stata have dealt with similar issues in the past (particularly important with storage of data => algorithms for large numbers to not take up too much memory) but have generally been able to resolve this problem.  Looking at how they have dealt with these issues could lead to a possible solution.
Comment 31 Tomasz Chmielewski 2009-11-23 14:26:30 UTC
Same with a recent SVN snapshot:

=0.01+0.2

0.21000000000000002


$ krunner -v
Qt: 4.6.0
KDE: 4.3.75 (KDE 4.3.75 (KDE 4.4 >= 20091113))
Run Command Interface: 0.1


kcalc calculates correctly.
Comment 32 Pino Toscano 2009-12-27 00:46:11 UTC
*** Bug 211052 has been marked as a duplicate of this bug. ***
Comment 33 Jacopo De Simoi 2010-01-09 17:31:28 UTC
SVN commit 1072213 by jacopods:

Round off the last digit to avoid giving wrong results for simple computations
BUG: 167986


 M  +20 -2     calculatorrunner.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=1072213
Comment 34 Jacopo De Simoi 2010-01-09 17:32:11 UTC
SVN commit 1072214 by jacopods:

round off the last digit to get rid of weird results performing simple computations
CCBUG:167986


 M  +20 -2     calculatorrunner.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=1072214
Comment 35 Jacopo De Simoi 2010-01-09 17:35:23 UTC
All testcases reported in this br have been checked and they work apart from:

595056260442243700 + 1 
The problem with this is that we don't have infinite precision integer arithmetics; 

If you want that, file a wish :)
Comment 36 Marco Poletti 2010-01-09 17:55:57 UTC
Well, if I understand it correctly my testcase would still not work (I posted it in bug 211052, a duplicate of this):

Enter 190.12 - 186.69 =

Expected result: 3.43

Actual result: 3.430000000000007
Actual result (after fix): 3.43000000000001


I think hiding only one digit is still too little, I don't think most people need 14 decimal digits anyway :-).
I would round hiding more digits (like 3, 5 or something like that) instead of the current 1 (after the fix).
Comment 37 Jacopo De Simoi 2010-01-09 18:00:37 UTC
SVN commit 1072222 by jacopods:

one more digit has to go, thanks Marco Poletti for pointing that out
CCBUG: 167986


 M  +1 -1      calculatorrunner.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=1072222
Comment 38 Jacopo De Simoi 2010-01-09 18:21:42 UTC
r1072229 is needed as well. sorry for the 3 episodes patch :P
Comment 39 Jacopo De Simoi 2010-01-09 22:15:43 UTC
r1072229 is needed as well. sorry for the 3 episodes patch :P
Comment 40 Alex 2010-02-14 14:45:00 UTC
KDE 4.3.5

=(700*(2^20))/(32*(2^10))
Expected result is 22400
Reported result is 2293760
Comment 41 Tomasz Chmielewski 2010-02-14 14:55:30 UTC
KDE 4.4.x outputs 22400.
Comment 42 Dario Andres 2010-03-30 14:55:27 UTC
*** Bug 232693 has been marked as a duplicate of this bug. ***
Comment 43 Christoph Feck 2010-04-14 02:40:13 UTC
*** Bug 234097 has been marked as a duplicate of this bug. ***
Comment 44 m'r 2017-07-22 23:34:27 UTC
It is not completely fixed: 100.1 - 100 = 0.099999999999994

The problem is that 
Math.log(Math.abs(0.099999999999994))/Math.log(10)
is less than -1 and so the result with 14 significant digits is rounded to 15 digits :-( 

A simple fix could enclose it in Math.max(0,Math.floor(…)) but then: 
(100.1-100)*10=0.9999999999999
(100.01-100)*100=1.0000000000005
etc.
I think, there is no easy solution using Javascript (but I am not a programmer).

I would suggest to use another method to calculate the result; maybe make the qalculator default (and add a dependency on its lib) as it seems, that it handles this cases correct?
Comment 45 Christoph Feck 2017-07-31 12:29:11 UTC
Try "12345678901234.1-12345678901234" on any calculator that does use IEEE double precision. You will have to live with it, or use a program that either uses unlimited precision (based on libgmp), or BCD arithmetic.
Comment 46 m'r 2017-08-01 13:30:05 UTC
(In reply to Christoph Feck from comment #45)
> Try "12345678901234.1-12345678901234" on any calculator that does use IEEE
> double precision. You will have to live with it, or use a program that
> either uses unlimited precision (based on libgmp), or BCD arithmetic.

But as a user, I am not expecting rounding errors for “simple” numbers. By rounding to less digits (e.g. 7 for IEEE double precision) the errors could be hidden for more cases. I suggest a quick patch for javascript at:
https://phabricator.kde.org/D6845

It would better to use another method than javascript-evaluate. Why do not make libqalculate a dependency?

From a users perspective, the calculator should yield exact results whenever possible. This includes floating point numbers. Exceptions could be roots and functions, e.g., sin and log.
Comment 47 Sasha Unspecified 2017-08-01 14:36:38 UTC
I was subscribed to this report, because another report was marked as duplicate of this one.
But that other report was about a more important thing: some formulas were evaluated absolutely wrong, e.g. =(2^2)^1 returned 1; now that bug is de-facto resolved.
So now I unsubscribe.