编辑这个页面须要登录或更高权限!
pow()函数计算幂。
此函数在<cmath>头文件中定义。
[数学] baseexponent = pow(base, exponent) [C++ 语言]
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); // 对于其他参数类型
从C ++ 11开始,如果传递给pow()的参数为long double,则返回类型Promoted 为long double。如果不是,则返回类型Promoted为double。
pow()函数采用两个参数:
base -基数
exponent -基数的指数
pow()函数返回以幂为底的底数。
#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; }
运行该程序时,输出为:
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; //两个参数都是int // pow()在本示例中返回double int intBase = -4, intExponent = 6; double answer; answer = pow(intBase, intExponent); cout << intBase << "^" << intExponent << " = " << answer; return 0; }
运行该程序时,输出为:
4.4^-3 = 0.0117393 -4^6 = 4096