1 /************************************************************************* 2 > File Name: 09_Power.c 3 > Author: Juntaran 4 > Mail: [email protected] 5 > Created Time: 2016年08月29日 星期一 21时07分21秒 6 ************************************************************************/ 7 8 #include <stdio.h> 9 10 double Power(double base, int exponent) 11 { 12 if (base == 0) 13 return 0; 14 if (exponent == 0) 15 return 1; 16 if (exponent == 1) 17 return base; 18 19 int x = abs(exponent); 20 double r = 1.0; 21 22 while (x) 23 { 24 if (x & 1) 25 r *= base; 26 base *= base; 27 x >>= 1; 28 printf("r is %f\n base is %f\n x is %d\n", r, base, x); 29 } 30 return exponent<0 ? 1/r : r; 31 } 32 33 int main() 34 { 35 double base = 2; 36 int exponent = 10; 37 double ret = Power(base, exponent); 38 printf("ret is %f\n", ret); 39 40 return 0; 41 }
时间: 2024-10-21 00:45:24