标签:
1 /************************************************************************* 2 > File Name: 09_Power.c 3 > Author: Juntaran 4 > Mail: JuntaranMail@gmail.com 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 }
标签:
原文地址:http://www.cnblogs.com/Juntaran/p/5819697.html