博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言幂函数_了解C / C ++中的幂函数
阅读量:2532 次
发布时间:2019-05-11

本文共 3128 字,大约阅读时间需要 10 分钟。

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 
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 
#include
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 
#include
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 
#include
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 on C programming!

对于类似的内容,请阅读我们有关C编程的 !

参考资料 (References)

  • on the power() function in C

    C中power()函数上的


翻译自:

c语言幂函数

转载地址:http://dmozd.baihongyu.com/

你可能感兴趣的文章
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>