Home  >  Article  >  Backend Development  >  How to express exponentiation in C language

How to express exponentiation in C language

小老鼠
小老鼠Original
2024-04-11 14:06:17792browse

There are two ways to express power in C language: using the pow() function, which receives the base and exponent and returns the result; or using the power operator (**), which directly calculates the power of the base and exponent.

How to express exponentiation in C language

Representation of exponents in C language

In C language, exponents can be expressed in the following two ways :

1. Use the pow() function

pow() The function receives two real number parameters: base and Index and return the result.

<code class="c">#include <math.h>

double result = pow(2.0, 3.0); // 计算 2 的 3 次方</code>

2. Use Power operator (**)

Power operator* * Directly calculate the power of two numbers. The number on the left is the base and the number on the right is the exponent.

<code class="c">double result = 2.0 ** 3.0; // 计算 2 的 3 次方</code>

Example:

<code class="c">#include <stdio.h>
#include <math.h>

int main() {
  double x = 2.0;
  double y = 3.0;

  // 使用 pow() 函数计算 x 的 y 次方
  double result1 = pow(x, y);

  // 使用乘方运算符计算 x 的 y 次方
  double result2 = x ** y;

  printf("x 的 y 次方,使用 pow() 函数:%f\n", result1);
  printf("x 的 y 次方,使用乘方运算符:%f\n", result2);

  return 0;
}</code>

Output:

<code>x 的 y 次方,使用 pow() 函数:8.000000
x 的 y 次方,使用乘方运算符:8.000000</code>

The above is the detailed content of How to express exponentiation in C language. 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