I tried to calculate this way, only that pow
returns only the number 1 for me:
int funcao1(int y,int z){
int k;
k = pow(y,1/z);
return(k);
}
I tried to calculate this way, only that pow
returns only the number 1 for me:
int funcao1(int y,int z){
int k;
k = pow(y,1/z);
return(k);
}
Try this:
double funcao1(int y,int z){
double k;
k = pow(y,1.0/z);
return(k);
}
Or even better:
double raiz(int radicando, int indice){
return pow(radicando, 1.0/indice);
}
What has changed:
1) The root of a number can be fractional, so you need to return float
or double
otherwise there will be rounding.
2) Because% is an integer and you were z
, this result will also be integer, since the 1/z
is treated as integer and C does not automatically promote the result of a division of integers for 1
or float
. If you passed, say 4 and 2, the function would raise 4 to 1/2, but 1/2 is zero when we convert to integer (rounding down). Anything raised to zero is 1, so the constant result you were getting. The workaround is to use double
instead of 1.0
, which forces the result type to be 1