Thread: [Dev-C++] numerical error in a simple program
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Luís B. <lf...@ya...> - 2003-09-03 08:17:05
|
When I try to compile this program:
********************************************
#include <stdio.h> // printf
#include <stdlib.h> // system
int main(int argc, char *argv[])
{
double a;
a = 0.3333;
printf( " a = %15.15f\n", a );
a *= 10;
printf( " a = %15.15f\n", a );
a *= 10;
printf( " a = %15.15f\n", a );
a *= 10;
printf( " a = %15.15f\n", a );
a *= 10;
printf( " a = %15.15f\n", a );
system("PAUSE");
return 0;
}
********************************************
I get this output:
********************************************
a = 0.333300000000000
a = 3.333000000000000
a = 33.329999999999998
a = 333.299999999999950
a = 3332.999999999999500
********************************************
How to avoid this error?
Thank in advance for any response.
=====
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Luís Filipe Delgado Brás
Eng.º Civil
Porto - Portugal
email: lf...@ya...
http://www.fotopt.net/autor.asp?autor=2637
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
|
|
From: Neszt T. <ne...@ss...> - 2003-09-03 08:38:12
|
> ******************************************** > a = 0.333300000000000 > a = 3.333000000000000 > a = 33.329999999999998 > a = 333.299999999999950 > a = 3332.999999999999500 > ******************************************** > > How to avoid this error? It is error? Hmm.. I guess, It is intelligence :-)))) Tibor |
|
From: Neszt T. <ne...@ss...> - 2003-09-03 08:45:34
|
Instead of:
> printf( " a = %15.15f\n", a );
Try:
printf( " a = %15.10f\n", a );
:-)
Tibor
|
|
From: Luís B. <lf...@ya...> - 2003-09-03 10:30:07
|
--- Neszt Tibor <ne...@ss...> wrote: > Instead of: > > > printf( " a = %15.15f\n", a ); > > Try: > printf( " a = %15.10f\n", a ); > > :-) > > Tibor > It's not a question of output. Numerically the error is still there: ********************************* printf( "(int)a = %d\n", (int)a); ********************************* output -> 3332 ===== +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= Luís Filipe Delgado Brás Eng.º Civil Porto - Portugal email: lf...@ya... http://www.fotopt.net/autor.asp?autor=2637 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= __________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com |
|
From: w.y. <yok...@ve...> - 2003-09-03 10:42:34
|
"Luís Brás" wrote: > > a = 0.333300000000000 > a = 3.333000000000000 > a = 33.329999999999998 > a = 333.299999999999950 > a = 3332.999999999999500 > How to avoid this error? You have to design your own floating point format type, or find a library that does it... If you need _exact_ FP calculations, the built in types can't do it. |
|
From: <ten...@ne...> - 2003-09-03 16:11:28
|
The binary representation for floating point numbers cannot be perfectly accurate, so that imprecissions. With double variables you get better precission that simple float. But if you want more precission, then, as said by w.y., there shall be special libraries. w.y. wrote: >"Luís Brás" wrote: > > >> a = 0.333300000000000 >> a = 3.333000000000000 >> a = 33.329999999999998 >> a = 333.299999999999950 >> a = 3332.999999999999500 >> >> > > > >>How to avoid this error? >> >> > >You have to design your own floating point format type, or find a >library that does it... >If you need _exact_ FP calculations, the built in types can't do it. > > >------------------------------------------------------- >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 > > > |
|
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
>
|