Rounding Error
Eric Mercer
eemercer@yuan.elen.utah.edu
Wed Sep 16 12:02:00 GMT 1998
I have run into an interesting situation using the following
configuration:
* gcc version egcs-2.90.27 980315 (egcs-1.0.2 release)
shipped with RedHat 5.1
* Pentium II 266 Mhz running RedHat 5.1
When I compile and run the following snippet of code:
#include <iostream.h>
#include <math.h>
main( int argc, char *argv[] ) {
double tolerance = 0.001;
cout << endl << "ceil(1/" << tolerance << ") -> " << ceil( 1.0/tolerance);
cout << endl << "floor(1/" << tolerance << ") -> "<< floor(1.0/tolerance );
cout << endl << "(int)(1/" << tolerance << ") -> "
<< (int)((double)1.0/tolerance );
cout << endl;
tolerance = 0.01;
cout << endl << "ceil(1/" << tolerance << ") -> " << ceil( 1.0/tolerance);
cout << endl << "floor(1/" << tolerance << ") -> "<< floor(1.0/tolerance );
cout << endl << "(int)(1/" << tolerance << ") -> "
<< (int)((double)1.0/tolerance );
cout << endl;
tolerance = 0.1;
cout << endl << "ceil(1/" << tolerance << ") -> " << ceil( 1.0/tolerance);
cout << endl << "floor(1/" << tolerance << ") -> "<< floor(1.0/tolerance );
cout << endl << "(int)(1/" << tolerance << ") -> "
<< (int)((double)1.0/tolerance );
cout << endl;
}
I get the following incorrect results:
ceil(1/0.001) -> 1000
floor(1/0.001) -> 1000
(int)(1/0.001) -> 999
ceil(1/0.01) -> 100
floor(1/0.01) -> 100
(int)(1/0.01) -> 99
ceil(1/0.1) -> 10
floor(1/0.1) -> 10
(int)(1/0.1) -> 9
Now, Is this a problem in glibc, egcs, or the Pentium II hardware?
egm
More information about the Gcc-bugs
mailing list