c语言幂函数_了解C / C ++中的幂函数

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)



翻译自: https://www.journaldev.com/40777/power-function-in-c-plus-plus

c语言幂函数

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值