Machine error demonstration
Either there is a problem with printf, atof, division or with floating point numbers:
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(int argc, char **argv){
if(argc>2){
float f1 = atof(argv[1]);
float f2 = atof(argv[2]);
printf("%f/%f = %f\n", f1, f2, f1/f2);
}
return 0;
}
After compiling and running the given code, I get these strange results:
$ ./div 234 67.9
234.000000/67.900002 = 3.446244
$ ./div 234 67.2
234.000000/67.199997 = 3.482143
The code was compiled with g++ (GCC) 4.1.1 20060724 (prerelease) (4.1.1-3mdk). No optimizations were used:
$ g++ div.cpp -o div
Speaking of division, this article about division by zero might be interesting.
No comments:
Post a Comment