Home > Article > Backend Development > How to express the power function in c++
In C, the pow() function is used to calculate the power value. It accepts two parameters: base and exponent, and returns the base raised to the power of the exponent. The pow() function can only exponentiate double-precision floating-point numbers and will produce specific results when the base or power is a negative number.
Power functions in C
In C you can use pow()
Function exponentiates two numbers. This function accepts two parameters:
#base
: The base to be exponentiated. exponent
: The exponent to be raised to the power of the base. pow()
The function will return base
raised to the exponent
power.
Grammar:
<code class="cpp">double pow(double base, double exponent);</code>
Example:
<code class="cpp">#include <iostream> #include <cmath> using namespace std; int main() { // 计算 2 的 5 次幂 double result = pow(2, 5); // 打印结果 cout << result << endl; // 输出:32 return 0; }</code>
Note:
pow()
The function can only exponentiate double-precision floating-point numbers. If you need to exponentiate an integer, you can use the powl()
function, which accepts an argument of type long double. pow()
The function returns NaN (not a number) when the base is negative. pow()
function will return the reciprocal power of the base. For example, pow(2, -2)
returns 1/4. The above is the detailed content of How to express the power function in c++. For more information, please follow other related articles on the PHP Chinese website!