數值的整數次方

2021-08-28 13:39:43 字數 745 閱讀 4695

我們看到這個題目時候,第一時間就會寫出如下的**:

double power(double base, int exponent)

return result;

}

不過很遺憾,這樣的寫法雖然也對,但是**完整嗎?

當base=0、exponent<1時,0的負數次方無意義、0的0次方在數學上是無意義的,但是必須考慮到這個邊界值。

當指數為負數,也沒有考慮。

0的正數次方是0,是有意義的,所以不能和無意義返回0混淆,我們建立需要全域性變數。

考慮所有情況,對上**進行修改:

#define exp 0.000000000000001

bool judge = false;

double power(double base,int exponent)

if (exponent < 0)

exponent1 = -exponent;

else

exponent1 = exponent;

for (i = 1; i <= exponent1; i++)

if (exponent < 0)

result = 1.0 / result;

return result;

}

數值整數次方

題目 實現函式double power double base,int exponent 求base的exponent次方。不得使用庫函式,同時不需要考慮 大數問題。includebool equal double num1,double num2 double powerwithunsignede...

數值整數次方

題目 實現函式double power double base,int exponent 求base的exponent次方。不得使用庫函式,同時不需要考慮 大數問題。includebool equal double num1,double num2 double powerwithunsignede...

數值的整數次方

題目 實現函式double power double base,int exponent 求base的exponent次方。不得使用庫函式,同時不需要考慮大樹問題。這道題目有以下幾點需要注意 0的0次方是無意義的,非法輸入 0的負數次方相當於0作為除數,也是無意義的,非法輸入 base如果非0,如果...