pow()函數(shù)計(jì)算冪。
此函數(shù)在<cmath>頭文件中定義。
[數(shù)學(xué)] baseexponent = pow(base, exponent) [C++ 語(yǔ)言]
double pow(double base, double exponent); float pow(float base, float exponent); long double pow(long double base, long double exponent); Promoted pow(Type1 base, Type2 exponent); // 對(duì)于其他參數(shù)類(lèi)型
從C ++ 11開(kāi)始,如果傳遞給pow()的參數(shù)為long double,則返回類(lèi)型Promoted 為long double。如果不是,則返回類(lèi)型Promoted為double。
pow()函數(shù)采用兩個(gè)參數(shù):
base -基數(shù)
exponent -基數(shù)的指數(shù)
pow()函數(shù)返回以冪為底的底數(shù)。
#include <iostream> #include <cmath> using namespace std; int main () { double base, exponent, result; base = 3.4; exponent = 4.4; result = pow(base, exponent); cout << base << "^" << exponent << " = " << result; return 0; }
運(yùn)行該程序時(shí),輸出為:
3.4^4.4 = 218.025
#include <iostream> #include <cmath> using namespace std; int main () { long double base = 4.4, result; int exponent = -3; result = pow(base, exponent); cout << base << "^" << exponent << " = " << result << endl; //兩個(gè)參數(shù)都是int // pow()在本示例中返回double int intBase = -4, intExponent = 6; double answer; answer = pow(intBase, intExponent); cout << intBase << "^" << intExponent << " = " << answer; return 0; }
運(yùn)行該程序時(shí),輸出為:
4.4^-3 = 0.0117393 -4^6 = 4096