📜  C++ pow()

📅  最后修改于: 2020-09-25 08:01:15             🧑  作者: Mango

pow() 函数计算一个升为指数幂的基数。

此函数在头文件中定义。

[Mathematics] baseexponent = pow(base, exponent) [C++ Programming]

pow()原型[从C++ 11标准开始]

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 。如果不是,则返回类型Promoteddouble

pow()参数

pow() 函数采用两个参数:

pow()返回值

pow() 函数返回以幂为底的底数。

示例1:pow()如何在C++中工作?

#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

示例2:具有不同参数组合的pow()

#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