Re: Huge Floating Point Error
- From: loftusoft@xxxxxxxxx
- Date: 2 Nov 2006 04:52:26 -0800
I understand now. Thank you for your help.
Mike
Bruno van Dooren [MVP VC++] wrote:
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.
Floats (4 byte) have only 7 digits of precision
subtracting 58331.2 from 58331.3 and ending up with 0.101563 is correct,
because the last part '1563' starts from the 8th digit of precision.
floats are simply too small for what you want to do. You should either use
doubles, or multiply your result by 10, truncate it to only its integer part
and then divide by 10 again.
Another symptom of this behaviour: if you have to add a collection of
floats, the order in which you add them determines the outcome.
starting with the smallest values first will yield the most precise answer.
doing it any other way will give larger errors.
These things are implied by the floating point standard itself. The fact
that VC6 yield other results is because it does floating point stuff
differently from later compilers. But both are correct as far as floating
point behavior is concerned.
--
Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@xxxxxxxxxxx
Remove only "_nos_pam"
.
- References:
- Huge Floating Point Error
- From: loftusoft
- Re: Huge Floating Point Error
- From: Bruno van Dooren [MVP VC++]
- Re: 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: Huge Floating Point Error
- Next by Date: Re: WHy is C# so much slower than c++???
- Previous by thread: Re: Huge Floating Point Error
- Next by thread: Re: question related to sockets
- Index(es):
Relevant Pages
|