What is the usage of the pow function in C++?
In C++, the `pow()` function is used to calculate an exponent of a number. Here is how it is used:
#include <cmath>double pow(double base, double exponent);
Base: The number being calculated.
exponent: the power to which a number is raised to.
This function returns the result of raising `base` to the power of `exponent`. It is important to note that the parameters and return value of the `pow()` function are both of type `double`, so it can handle floating-point numbers.
Here are some examples:
#include <iostream>#include <cmath>
int main() {
double result;
// 计算3的4次方
result = pow(3, 4);
std::cout << "3^4 = " << result << std::endl;
// 计算2的平方根
result = pow(2, 0.5);
std::cout << "Square root of 2: " << result << std::endl;
return 0;
}
Output:
3^4 = 81Square root of 2: 1.41421
Please note that the `pow()` function returns a floating point number of type `double`, if you need to convert it to an integer, you can use type casting or other methods to do so.