Re: Huge Floating Point Error

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



My mistake - hopefully this will make it more clear:

#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
float f1 = (583312.0F * 0.1F);
cout << f1 << endl;
float f2 = (583313.0F * 0.1F);
cout << f2 << endl;
float f3 = f2 - f1;
cout << f3 << endl;
return 0;
}

f3 is 0.101563 instead of "0.1". I can accept the normally miniscule
epsilon. However, this is unacceptable - thus the statement of "Huge"
in regards to the error.


Mike




Bruno van Dooren [MVP VC++] wrote:
While debugging the following code:

#include <iostream>
using namespace std;
int main()
{
float f = 583312.0F * 0.1F;
cout << f << endl;
return 0;
}

The value of "f" as reported by the debugger is "58331.199" instead of
"58331.2". Note that the console output will correctly display
"58331.2". However, this is not just a "debugger-display" problem, this
problem was found since the errant data is actually being output to
Matlab data files.

I've tested this sample code on VC++ 6, 2002, and 2005. Both 2002 and
2005 exhibit this problem. Version 6 at least shows the correct result
while debugging.

There is no correct result. your value probably is 58331.1999999 and cout
probably truncates it after the nth digit.
floating point math is different from integer math.

google for 'what every programmer should know about floating point' and the
first n hits will explain exactly why you are having this problem, why it is
not bug, and why there is nothing you can do about it.


--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@xxxxxxxxxxx
Remove only "_nos_pam"

.



Relevant Pages

  • [PATCH][PPC64] Handle altivec assist exception properly
    ... This is the PPC64 counterpart of the PPC32 Altivec assist exception ... Fortunately there are only a limited set of altivec instructions ... +static unsigned int eexp2 ... +/* Round to floating integer, ...
    (Linux-Kernel)
  • Re: input and output questions about file
    ... using namespace std; ... junk.c:5: warning: type defaults to `int' in declaration of `std' ... junk.c:10: `string' undeclared ... junk.c:12: parse error before '/' token ...
    (comp.lang.c)
  • Re: using MFC VC++ - which is more efficient - float or double?
    ... Float has other nasty implications. ... who keep insisting that binary floating point should behave exactly like decimal numbers. ... instructions can create different performance. ... I'm seeing on the order of 25x faster 32 bit int timing. ...
    (microsoft.public.vc.mfc)
  • Re: Comparing files (Fast)
    ... I didn't knew what is was, but using fseek solved the problem. ... I'm using a timer utility Scott Meyers wrote for use ... > using namespace std; ... > int ch1,ch2; ...
    (comp.lang.cpp)
  • [PATCH][PPC32] Handle altivec assist exception properly
    ... On machines with Altivec, ... don't have to emulate the whole altivec instruction set. ... +static unsigned int eexp2 ... +/* Round to floating integer, ...
    (Linux-Kernel)