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 CC中power()函数上的Linux手册页


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

c语言幂函数

c语言幂函数_了解C / C ++中的幂函数相关推荐

  1. 如何查看笔记本的语言编码_在编码笔记本电脑中寻找什么

    如何查看笔记本的语言编码 Did you just graduate from your coding bootcamp and now have to return your loaner lapt ...

  2. python如何用c语言表示_如何在python中调用C语言代码

    原博文 2019-04-03 14:19 − 1.使用C扩展CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码 开发者有三种方法可以在自己的Python代码中来调用C编写 ...

  3. python画指数函数图像_解决python中的幂函数、指数函数问题

    最近在调代码,碰到幂函数.指数函数,总是提示 ValueError: math domain error ValueError: negative number cannot be raised to ...

  4. python中幂函数_解决python中的幂函数、指数函数问题

    最近在调代码,碰到幂函数.指数函数,总是提示 ValueError: math domain error ValueError: negative number cannot be raised to ...

  5. python自然对数为底的指数函数_解决python中的幂函数、指数函数问题

    最近在调代码,碰到幂函数.指数函数,总是提示 ValueError: math domain error ValueError: negative number cannot be raised to ...

  6. python幂函数_解决python中的幂函数、指数函数问题

    最近在调代码,碰到幂函数.指数函数,总是提示 ValueError: math domain error ValueError: negative number cannot be raised to ...

  7. 关于python浮点数类型错误的是_关于Python语言的浮点数类型,以下选项中描述错误的是...

    [单选题]关于Python字符串,以下选项中描述错误的是 [单选题]以下程序的输出结果是: lcat =["狮子","猎豹","虎猫",&q ...

  8. python 幂函数 幂为小数_解决python中的幂函数、指数函数问题

    最近在调代码,碰到幂函数.指数函数,总是提示 ValueError: math domain error ValueError: negative number cannot be raised to ...

  9. python要求所有浮点数必须带有小数部分对吗_关于 Python 语言的浮点数类型,以下选项中描述错误的是( )_学小易找答案...

    [简答题]请上传practice 2 完成图片 [填空题]作用在梁上的载荷一般可分为().().()三种. [填空题]梁根据约束有().().()三种基本类型. [单选题]关于 Python 的复数类 ...

  10. c语言判断s1是否大于s2,C语言程序设计_复习资料一.doc

    C语言程序设计_复习资料一 -- <程序设计基础> 院(系) 班级 学号 姓名 试卷卷面成绩占课程考核成绩 %平时 成绩占 %课程考核成绩题号一二三四五六七八九十小计得分 得 分一.单项选 ...

最新文章

  1. JavaEE 的基本实现
  2. Undefined、Null区别渗透
  3. esxi服务器3d性能,ESXi主机性能问题(示例代码)
  4. 【自适应(盲)均衡6】信号过多径衰落信道的矩阵乘法表示之Toeplitz矩阵和Toeplitz块矩阵的生成(分数间隔FSE)
  5. web服务面试python_Python面试重点(web篇)
  6. 正则匹配 表情,表情
  7. 40-400-040-运维-优化-MySQL File Sort 原理以及优化
  8. @WebService作用
  9. python django博客源码_利用Django实现一个博客(附全部源码)
  10. Hibernate一级缓存、二级缓存以及查询缓存的关系
  11. pic16f616单片机C语言编程,PIC16F616型单片机介绍
  12. java 字符串排列组合_java实现字符串排列组合问题
  13. 适合学计算机用的机械键盘,一款好用的机械键盘应该怎么选?看完这篇就明白了...
  14. 人工智能助力三维几何自动化建模
  15. 自我营销(转帖自 TI E2E 工程师社区 (Beta))
  16. 【Robo3T】为mongodb数据库中的表创建索引的步骤
  17. Excel怎么设置每页都打印标题行?
  18. Ubuntu 16.04 安装 CUDA Toolkit 10.0
  19. Oracle分区表的使用
  20. leetCode简单算法之排排坐,分糖果

热门文章

  1. 元宇宙虚拟人迎来高峰期,哪个是你的最爱?
  2. 网络逻辑漏洞、SQLMAP、Nmap及其它命令的使用方法
  3. 《实用python程序设计》练习题:向量点积计算
  4. javaScript学习笔记【尚硅谷】
  5. python编程实现华氏温度和摄氏温度转换
  6. Linux技术——lsof命令详解
  7. android旅游视频播放,VPlayer视频播放器
  8. 日剧推介:怨み屋本舗
  9. 路由器运行python脚本_在路由器中运行Python
  10. 鹰眼系统原理_飞思卡尔智能车一:山外鹰眼摄像头使用原理