c语言幂函数
In this article, we’ll take a look at understanding the power function in C / C++.
在本文中,我们将了解C / C ++中的幂函数。
The power function computes the power of a base, raised to an exponent number.
幂函数计算底数的幂,并提高到指数数。
Let’s look at this function in a bit more detail, using some examples.
让我们使用一些示例来更详细地了解此功能。
C / C ++中Power函数的基本语法 (Basic Syntax of the Power function in C/C++)
The pow()
function takes in a base number and an exponent number, and returns the value base^(exponent)
.
pow()
函数接受一个基数和一个指数,并返回值base^(exponent)
。
All of these values are of the type double
.
所有这些值都是double
类型的。
Also, this function is a part of the <math.h>
header file, so we must import it first.
另外,此函数是<math.h>
头文件的一部分,因此我们必须首先将其导入。
#include <math.h>
double pow(double base, double exponent);
In case we give an incorrent range for the input, we will get a NAN
result.
如果输入的范围不正确,我们将得到NAN
结果。
For example, if base
is a negative finite value, and exponent
is a finite non-integer, we will get a domain error, since the decimal power of a negative number is a complex number, which is not in the scope of C datatypes.
例如,如果base
是一个负的有限值,而exponent
是一个有限的非整数,我们将得到一个域错误,因为负数的十进制幂是一个复数,这不在C数据类型的范围内。
Let’s take a look at some examples now.
现在让我们看一些示例。
在C / C ++中使用Power函数–一些示例 (Using the Power function in C / C++ – Some Examples)
Let’s take two integers first, and find the power of them.
让我们先取两个整数,然后求出它们的幂。
#include <stdio.h>
#include <math.h>
int main() {
int base = 3;
int exponent = 5;
int result = (int) pow(base, exponent);
printf("Base = %d, Exponent = %d, Result = %d\n", base, exponent, result);
return 0;
}
Output
输出量
Base = 3, Exponent = 5, Result = 242
As you can see, pow()
did compute 3^5 = 243
.
如您所见, pow()
确实计算了3^5 = 243
。
Let’s check it for floating point numbers now.
现在让我们检查一下浮点数。
#include <stdio.h>
#include <math.h>
int main() {
double base = 3.45;
double exponent = 5.6;
double result = pow(base, exponent);
printf("Base = %.4lf, Exponent = %.4lf, Result = %.4lf\n", base, exponent, result);
return 0;
}
Output
输出量
Base = 3.4500, Exponent = 5.6000, Result = 1027.5121
Indeed, it seems to work with floating point exponents and bases as well!
确实,它似乎也适用于浮点指数和基数!
Let’s take another example, which will give us a NAN
result.
让我们再举一个例子,它将NAN
结果。
include <stdio.h>
#include <math.h>
int main() {
double base = -1;
double exponent = 5.6;
double result = pow(base, exponent);
printf("Base = %.4lf, Exponent = %.4lf, Result = %.4lf\n", base, exponent, result);
return 0;
}
Output
输出量
Base = -1.0000, Exponent = 5.6000, Result = -nan
Here, since -1^5.6
is a complex number, it will become a nan
value! So you must be very careful to ensure that your input and output aren’t nan
values!
在这里,由于-1^5.6
是复数,因此它将成为nan
值! 因此,您必须非常小心以确保您的输入和输出不是nan
值!
结论 (Conclusion)
We learned about using power()
in C / C++, which is useful to compute the mathematical power of a base, to an exponent.
我们学习了在C / C ++中使用power()
,该方法对于计算基数的数学功效非常有用。
For similar content, do go through our tutorial section on C programming!
对于类似的内容,请阅读我们有关C编程的教程部分 !
参考资料 (References)
- Linux Manual page on the power() function in C C中power()函数上的Linux手册页
翻译自: https://www.journaldev.com/40777/power-function-in-c-plus-plus
c语言幂函数