C语言:求X的Y次方

double MyPow(int x,int y)

{

unsigned int z = (y>=0 ? y : -y);

 

for(int tmp = 1; ;x *= x)//x=5

{

if((z&1) != 0)

{

tmp *= x;

}

if((z>>=1) == 0)

{

return y>=0 ? tmp : 1.0/tmp;

}

}

/*unsigned int z = (y>=0? y : -y);

int tmp = 1;

for(int i=0;i

{

tmp *= x;

}

return y>=0 ? tmp : 1.0/tmp;*/

}

 

int main()

{

printf("%f\n",MyPow(10,6));

printf("%f\n",MyPow(10,9));

printf("%f\n",MyPow(10,-2));

return 0;

}

你可能感兴趣的:(c语言)