I found some interesting behavior when discussing the pow function from Math.h with someone. I am well aware of the need to compile with the -lm flag, but to my surprise this actually isn't necessary if the arguments to the pow function are literals. For example, this program compiles in gcc
without using -lm:
Code:
#include <stdio.h>
#include <math.h>
int main()
{
double b = 2.0;
double e = 3.0;
double bCubed = pow(2.0, 3.0);
printf("%f cubed is %f\n", b, bCubed);
return 0;
}
If I try to change the call to any of these:
Code:
double bCubed = pow(2.0, e);
Code:
double bCubed = pow(b, 3.0);
Code:
double bCubed = pow(b, e);
Then the -lm option is required or else it can't compile.
So does anyone know why it's necessary to use -lm when the argument(s) is/are variable(s) but it works fine if both arguments are literal values?
As a side note, any of the above compile with g++ however. Also, if it helps, this is the version of gcc I'm using:
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
This doesn't happen on my Mac however that is running: i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5484)