卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章17126本站已运行3324

在C/C++中的Power函数

在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# 和多重继承
下一篇: 返回列表

相关推荐

留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏