-
exponential question
Code:
#include <stdio.h>
#include <math.h>
main ()
int a=2;int b=5; int c; c = pow(a,b);
printf("%d to the power of %d is equal to %d\n",a,b,c);
}
this program called ma.c is simply trying to output: 2 raise to
the power of 5, but i get this compilation error below, can anyone tell me what did i missed here, im new to c programming.
bt ~ $ gcc -o ma ma.c
/tmp/cck7y346.o(.text+0x3f): In function `main':
: undefined reference to `pow'
collect2: ld returned 1 exit status
-
Add "-lm" to include the "libm" library to your build - it is not included by default, because most systems don't use the math library - so if you DO need it, you have to ask for it "by hand".
Also, if you want 2 ^^ 5, you can do that by "1 << 5", rather than calling pow() which converts arguments to floating point, does expensive exponential functions - it's probably 30-100 times slower than the 1 << 5 method [which is one or two clock-cycles on most processors].
--
Mats
-
thanks
thanks mat, it works fine
bt ~ $ gcc -lm -o ma ma.c
bt ~ $ ./ma
2 to the power of 5 is equal to 32
-
Next time, add [code][/code] tags around the code.
Don't rip out all the braces to get rid of the pop up just so you can post.