首页 > 后端开发 > C++ > 正文

在C/C++中的Power函数

PHPz
发布: 2023-09-02 21:25:03
转载
1427 人浏览过

在C/C++中的Power函数

Power function is used to calculate the power of the given number.

The pow function find the value of a raised to the power b i.e. ab.

Syntax

double pow(double a , double b)
登录后复制

它接受双整数作为输入,并将双整数作为输出。它的pow()函数在math.h包中定义。

如果将整数传递给幂函数,函数会将其转换为双精度数据类型。但是这里存在一个问题,有时候这种转换可能会将其存储为较低的双精度数。例如,如果我们传递3并将其转换为2.99,那么平方是8.99940001,转换为8。但这是一个错误,虽然它很少发生,但为了消除这个错误,将其加上0.25。

示例代码

#include <stdio.h>
#include <math.h>
int main() {
   double x = 6.1, y = 2;
   double result = pow(x, y);
   printf("%f raised to the power of %f is %f \n" ,x,y, result );
   // Taking integers
   int a = 5 , b = 2;
   int square = pow(a,b);
   printf("%d raised to the power of %d is %d \n", a,b, square );
   return 0;
}
登录后复制

输出

6.100000 raised to the power of 2.000000 is 37.210000
5 raised to the power of 2 is 25
登录后复制

以上是在C/C++中的Power函数的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!