C语言二分法实现浮点数的整数幂次

double POW( double x, int y)
{
     
    if(x==0) return 0;//if x is zero
    if(y==0) return 1;//if y is zero
    if(y==1) return x;
    if(y<0)  return 1.0 / POW(x, -y);//if y is negtive
    double temp = POW(x, y>>1);
    double res = temp * temp;
    return y % 2 == 0 ? res : res * x; //if y is even
}

你可能感兴趣的:(C,C)