數值的整數次方

2021-07-10 16:02:42 字數 837 閱讀 8879

題目描述:

給定乙個double型別的浮點數base和int型別的整數exponent。求base的exponent次方。

double pow(double base,int exp)應該考慮特殊的情況:

1)    base == 0.0   && exp < 0    沒有意義,因為在求乙個數的負數次方的時候,是先求出這個數的整數次方,然後求        倒數。

2) base==0.0   && exp==0  也沒有意義 。

第二點:注意判斷兩個浮點數型別的大小,是不能直接用等號來判斷的。

bool isequal(double a,double b)//判斷兩個double 型別是否相等

else

return false;}

第三點:求 乙個數的 n (無符號整數)次方可以用遞迴來實現

double unsignedpow(double base, unsigned int exp)

這下我們可以完整的寫出我們的程式了

bool flags=true;

double pow(double base,int exponent)

if(isequal(base,1.0))

unsigned int exp;

if(exponent<0)//是負數,

else

//exp是乙個正數;

//double result=unsignedpow(base,exp);

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,如果...