平台: Leetcode in C
Implement pow(x, n), which calculates x raised to the power n (i.e. xn).
在case myPow(0.00001,2147483647) 出现stack overflow的情况,
其实第二个参数不到INT_MAX就会overflow了。 请问是不是递回太深的缘故,要怎么保证不会overflow?
谢谢
double myPow(double x, int n){
double output;
if(n>0)
{
if(n == 1 )
return x;
else
{
output = x*myPow(x,n-1);
}
}
else if(n == 0 )
return 1;
else
{
if(n == -1)
return 1/x;
else
output = (1/x)*myPow(x,n+1);
}
return output;
}