|
|
|
|
|
by petschge
2559 days ago
|
|
And computers have a hard time with that. Take the following example code: #include <math.h>
#include <stdio.h>
#include <stdlib.h>
double f(const double x) {
return pow(x, 1000) / pow(x, 998);
}
int main(int argc, char* argv[]) {
if(argc != 2) {
fprintf(stderr, "Usage: %s x\n", argv[0]);
exit(1);
}
const double x = atof(argv[1]); // demo code without error checking
printf("x = %g, f(x) = %g\n", x, f(x));
exit(0);
}
compile with gcc -Wall -g -o test test.c -lm
and run it petschge@localhost:~$ ./test 0
x = 0, f(x) = -nan
petschge@localhost:~$ ./test 1
x = 1, f(x) = 1
petschge@localhost:~$ ./test 2
x = 2, f(x) = 4
The fun thing is, when you compile with gcc -Wall -O3 -ffast-math -g -o test2 test.c -lm
you actually get petschge@localhost:~$ ./test2 0
x = 0, f(x) = 0
|
|