題目描述
給定一個double類型的浮點數(shù)base和int類型的整數(shù)exponent陷揪。求base的exponent次方杂穷。
注意點:
- double類型base的計算:首先將小數(shù)點后移到末尾。比如飞蚓,計算12.3廊蜒,將12.3轉(zhuǎn)換為123.0
- 計算123的k次方后,再按照比例還原該數(shù)
- 注意k小于0的情況
double Power(double base, int exponent) {
int count_10 = 0;
int exponent_flag = 1;
while(base-(int)base)
{
base = base * 10;
count_10++;
}
if(exponent < 0)
{
exponent_flag = -1;
}
exponent = exponent * exponent_flag;
int pow1 = pow((int)base,exponent);
//if(count_10 == 0)
int m = pow(10,count_10);
int k = pow(m,exponent);
double pow2 = (double)pow1/k;
if(exponent_flag == -1)
{
pow2 = 1.0/pow2;
}
return pow2;
}