Re: Huge Floating Point Error

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



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"

.



Relevant Pages

  • Re: Huge Floating Point Error
    ... because the last part '1563' starts from the 8th digit of precision. ... floats are simply too small for what you want to do. ... These things are implied by the floating point standard itself. ... that VC6 yield other results is because it does floating point stuff ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Strange LISP: [ http://www.newlisp.org ]
    ... > some of the CL/LISP features to how those same features are ... Floats only have finite precision. ... Wanton misuse of floating point arithmetic is terribly common. ...
    (comp.lang.lisp)
  • Re: format floats rationals question
    ... that returns "1,000.1" for floats and rationals? ... Note that the output is not 1,003,423.32423; this is due to floating ... Loses precision pretty fast. ... Unfortunately, using rationals for more ...
    (comp.lang.lisp)
  • Re: Speed Comparison Perl Python & C
    ... I am going to learn about Psyco. ... > doing the computations with floating point numbers and finally ... floats have 53 bits of precision. ...
    (comp.lang.python)
  • Re: Precision
    ... Whether one does the subsequent calculations in internal ie integer format ... I have always used external format with Precision 4 since my ... floating point calcs which are done in binary have around 3% error albeit at ... positions it is IMPOSIBLE to have a result of 4 decimals with the 2 ...
    (comp.databases.pick)