Home  >  Article  >  Backend Development  >  How to express the power function in c++

How to express the power function in c++

下次还敢
下次还敢Original
2024-05-06 18:12:14639browse

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.

How to express the power function in c++

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.
  • When the power is a negative number, the 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn