Bug 71979

Summary: kcalc cannot calculate correctly
Product: [Applications] kcalc Reporter: Andreas Hartmann <andihartmann>
Component: generalAssignee: Evan Teran <evan.teran>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: ---   
Platform: unspecified   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Andreas Hartmann 2004-01-06 15:39:43 UTC
Version:           1.3.999 (using KDE 3.1.94 (CVS >= 20031206), compiled sources)
Compiler:          gcc version 3.3.1
OS:          Linux (i686) release 2.4.24

Do the following easy calculation:
49.84 + 33.52 = 83.36

kcalc claims:
83.36000000000001

kcalc in kde 3.1 is working fine.
Comment 1 Thiago Macieira 2004-01-06 21:31:11 UTC
That's how floating point works. The numbers aren't absolutely precise.
Comment 2 Christian Loose 2004-01-07 12:55:24 UTC
Well, but I don't think that any other calculator exposes the problems of floating point math in this way. 

The other calculators I tried (both programs and real life) show 83.36 as the result.

Also this isn't a problem of C++ because doing a "cout << double(49.84 + 33.52) << endl" also outputs 83.36.
Comment 3 Thiago Macieira 2004-01-07 16:20:08 UTC
I know, but the manipulations done internally by kcalc make it lose a bit more precision than your simple C++ program. Or, quite possibly, kcalc is just more precise in its results than iostream's operator<<(double).

In any event, I agree with you it's ugly and annoying, but it's also not wrong from the floating point point of view: we're talking about a 1 in 10^19 error (meaning from a "double" point of view, they are the same number).

But, yea, if possible, we should get rid of this.
Comment 4 Andreas Hartmann 2004-01-07 18:22:34 UTC
Subject: Re:  kcalc cannot calculate correctly

Why doesn't have KDE 3.1.4 this problem? There are 16 digits selected, too.
I've never seen such a problem.

Comment 5 Thiago Macieira 2004-01-07 18:44:12 UTC
As I said, it depends on the internal manipulations of the floating point variables. 83.36 and 83.36000000000001 are the *exact* same representation in 'float' and 'double' representations, but not in 'long double'. It would seem that something changed the order of the calculations/manipulations -- or of the precision of the result -- which introduced this error.

Now, I don't know kcalc code and I don't know how to fix this. I want it fixed as much as you do, but I'm merely pointing out that imprecisions are always the by-product of limited precision floating point.
Comment 6 Chris Howells 2004-01-11 10:15:06 UTC
Can't reproduce on FreeBSD.  Broken libc?
Comment 7 Malte S. Stretz 2004-01-11 15:23:14 UTC
I can reproduce this when I increase the "Maximum number of digits" from the default 14 to 16. Maybe there was a reason why that default value was chosen ;-)
Comment 8 Waldo Bastian 2004-01-12 15:51:20 UTC
Fixed. 

QString::toDouble() only has DOUBLE precision, not LONG DOUBLE. Better not use it.
Comment 9 Andreas Hartmann 2004-01-12 20:20:33 UTC
Subject: Re:  kcalc cannot calculate correctly

It's running fine now!


Thank you very much,
kind regards,
Andreas Hartmann