📜  C++ STL-math.pow()函数(1)

📅  最后修改于: 2023-12-03 15:29:50.414000             🧑  作者: Mango

C++ STL math.pow() Function

The math.pow() function is a part of C++ STL math library. It is used to calculate the power of a given number. This function requires two arguments - the base and the exponent. The function returns the result of the base raised to the power of the exponent.

Syntax
double pow(double base, double exponent);
Parameters
  • base - the base number that will be raised to the power of exponent.
  • exponent - the exponent that the base will be raised to.
Return Value

The function returns the result of the base raised to the power of the exponent.

Example
#include <iostream>
#include <cmath>

using namespace std;

int main() {
   double base, exponent, result;
   cout << "Enter base: ";
   cin >> base;
   cout << "Enter exponent: ";
   cin >> exponent;
   result = pow(base, exponent);
   cout << base << " raised to the power of " << exponent << " is " << result << endl;
   return 0;
}

In this example, the user is prompted to enter the base and the exponent, and the pow() function is used to calculate the result. The result is then printed to the console.

Output
Enter base: 2
Enter exponent: 3
2 raised to the power of 3 is 8

Conclusion

The math.pow() function is a useful function in the C++ STL math library. It allows us to easily calculate the power of a given number with minimal code. However, it is important to note that this function may not be efficient for very large numbers or large exponents.