📅  最后修改于: 2020-09-25 08:01:15             🧑  作者: Mango
此函数在
[Mathematics] baseexponent = pow(base, exponent) [C++ Programming]
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); // For other argument types
从C++ 11开始,如果传递给pow()的任何参数为long double
,则返回的Promoted
类型为long double
。如果不是,则返回类型Promoted
为double
。
pow() 函数采用两个参数:
pow() 函数返回以幂为底的底数。
#include
#include
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
#include
using namespace std;
int main ()
{
long double base = 4.4, result;
int exponent = -3;
result = pow(base, exponent);
cout << base << "^" << exponent << " = " << result << endl;
// Both arguments int
// pow() returns double in this case
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