Re: Huge Floating Point Error
- From: loftusoft@xxxxxxxxx
- Date: 2 Nov 2006 03:55:26 -0800
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"
.
- Follow-Ups:
- Re: Huge Floating Point Error
- From: Bruno van Dooren [MVP VC++]
- Re: Huge Floating Point Error
- References:
- Huge Floating Point Error
- From: loftusoft
- Re: Huge Floating Point Error
- From: Bruno van Dooren [MVP VC++]
- Huge Floating Point Error
- Prev by Date: Re: question related to sockets
- Next by Date: Re: Nasty Problem
- Previous by thread: Re: Huge Floating Point Error
- Next by thread: Re: Huge Floating Point Error
- Index(es):
Relevant Pages
|