Re: [Dev-C++] numerical error in a simple program
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-09-03 17:03:05
|
Double-precision numbers, at least the normal 8-byte IEEE-compliant type
have about 15 valid digits. Since you emit 15 digits after the decimal
point, the last digits will be wrong as soon as the number gets larger
than 1. For single precision float, you have about 8 valid digits.
If you adjust number of decimals so that total number of digits are <=3D 15=
,
the output will be correctly rounded.
If you don't need a fixed number of decimals, but just want to emit a
floating-point number, you might look into %g instead. It will
automatically adjust number of decimals emitted.
If you need an integer, don't just try (int)fp since that will truncate
the number. If the value was expected to be 3.0 but was 2.9999999999999
the integer will be set to 2. Instead do my_int =3D (int)(fp + 0.5) to
correctly round 2.49999 -> 2, 2.500001 -> 3. If you really want to
truncate, you should still do my_int =3D (int)(fp + EPS) where EPS is a
small number compared to expected range of fp, i.e. if fp is 0-100, set
EPS =3D 1E-10 or something. Note that fp / EPS must be smaller than 1E15
(should preferably be 1E10 - 1E13 or therearound) or fp + EPS will be
identical to fp because of cancellation, i.e. EPS will be so much smaller
than fp that during the add, EPS will be rounded to zero.
/Per W
On Wed, 3 Sep 2003, Lu=EDs Br=E1s wrote:
> When I try to compile this program:
>
> ********************************************
> #include <stdio.h> // printf
> #include <stdlib.h> // system
>
> int main(int argc, char *argv[])
> {
> double a;
> a =3D 0.3333;
> printf( " a =3D %15.15f\n", a );
> a *=3D 10;
> printf( " a =3D %15.15f\n", a );
> a *=3D 10;
> printf( " a =3D %15.15f\n", a );
> a *=3D 10;
> printf( " a =3D %15.15f\n", a );
> a *=3D 10;
> printf( " a =3D %15.15f\n", a );
> system("PAUSE");
> return 0;
> }
> ********************************************
>
> I get this output:
>
> ********************************************
> a =3D 0.333300000000000
> a =3D 3.333000000000000
> a =3D 33.329999999999998
> a =3D 333.299999999999950
> a =3D 3332.999999999999500
> ********************************************
>
> How to avoid this error?
> Thank in advance for any response.
>
>
> =3D=3D=3D=3D=3D
> +=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=
=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D
> Lu=EDs Filipe Delgado Br=E1s
> Eng.=BA Civil
> Porto - Portugal
> email: lf...@ya...
> http://www.fotopt.net/autor.asp?autor=3D2637
> +=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=
=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D+=3D
>
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site design software
> http://sitebuilder.yahoo.com
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|