c语言pow的用法

2016-12-02

C语言中pow函数用于计算x的y次幂。下面我们来看看c语言pow的用法。

pow函数有以下几种的重载形式:

double pow(double X,int Y);

float pow(float X,float Y);

float pow(float X,int Y);

long double pow(long double X,long double Y);

long double pow(long double X,int Y);

使用的时候应合理设置参数类型,避免有多个“pow”实例与参数列表相匹配的情况。

其中较容易发生重载的是使用形如:

int X,Y;

int num=pow(X,Y);

这是一个比较常用的函数,但是编译器会提醒有多个“pow”实例与参数列表相匹配。

可以使用强制类型转换解决这个问题:num=pow((float)X,Y)

原型:extern float pow(float x, float y);

用法:#include

功能:计算x的y次幂。

说明:x应大于零,返回幂指数的结果。

举例:

// pow.c

#include

#include

#include

void main()

{

printf("4^5=%f",pow(4.,5.));

getchar();

}

#include

#include

void main( void )

{

double x = 2.0, y = 3.0, z;

z = pow( x, y );

printf( "%.1f to the power of %.1f is %.1f ", x, y, z );

}

Output

2.0 to the power of 3.0 is 8.0

更多相关阅读

最新发布的文章