1
(integer literal) divided by 2
(integer literal) asks for integer division (on the operator /
) which results in 0
. From that on, you are giving 0
to a function, pow(3)
, that converts your 0
into 0.0
(as a double
required by the function) and this is what you are calculating, x
to the power of 0.0
which is 1.0
.
Had you used
pow(x, (1.0/2.0)); /* there's a closing parenthesis missing in your sample code */
using floating point literals, instead of integer, the division should have been floating point, you got 0.5
as result and you should be calculating the square root of x
.
By the way, you have a function sqrt(3)
to do square roots, in the same library:
pru.c
#include <math.h>
#include <stdio.h>
/* ... */
int main()
{
double x = 625.0;
printf("square root of %.10f is %.10f\n", x, sqrt(x));
printf("%.10f to the power 1/2 is %.10f\n", x, pow(x, 1.0/2.0));
return 0;
}
Executing that code gives:
$ make pru
cc -O2 -Wno-error -Werror -o pru pru.c
$ pru
square root of 625.0000000000 is 25.0000000000
625.0000000000 to the power 1/2 is 25.0000000000
$ _