C语言pow函数

#include<stdio.h>
#include<math.h>
#include "stdlib.h"
void f(long n)
{printf("%f\t", pow(n,3));
printf("%lf\t", pow(n,3));
printf("%ld\t", (long)pow(n,3));
printf("结果是:%I64d\t",(long long int )pow(n,3));
printf("%d\n", (int)pow(n,3));
}
main()
{long i=5;
printf("%f\t", pow(5,3));
printf("%lf\t", pow(5,3));
printf("%ld\t", (long)pow(5,3));
printf("结果是:%I64d\t",(long long int )pow(5,3));
printf("%d\n", (int)pow(5,3));
system("pause");

printf("%f\t", pow(i,3));
printf("%lf\t", pow(i,3));
printf("%ld\t", (long)pow((int)i,3));
printf("结果是:%I64d\t",(long long int )pow(i,3));
printf("%d\n", (int)pow(i,3));
system("pause");

f(i);
system("pause");
}
这个程序为什么只有前一个输出答案正确,后面的都不正确呢?
我想问的是为什么只有第一组输出语句pow(5,3)输出结果为125,后面二组,输出类型不为实型的时候输出结果为124?

pow()函数用来求x的y次幂,x、y及函数值都是double型 ,其原型为:double pow(double x, double y)。

实例代码如下:

#include<stdio.h>

#include<math.h>

void main()

{

double x = 2, y = 10;

printf("%f\n",pow(x, y));

return 0;

}

扩展资料

在调用pow函数时,可能导致错误的情况:

如果底数 x 为负数并且指数 y 不是整数,将会导致 domain error错误。

如果底数 x 和指数 y 都是 0,可能会导致 domain error?错误,也可能没有;这跟库的实现有关。

如果底数 x 是 0,指数 y 是负数,可能会导致?domain error 或pole error 错误,也可能没有;这跟库的实现有关。

如果返回值 ret 太大或者太小,将会导致range error 错误。

错误代码:

如果发生 domain error 错误,那么全局变量 errno 将被设置为  EDOM;

如果发生 pole error 或 range error 错误,那么全局变量 errno 将被设置为 ERANGE。

参考资料:

pow函数——百度百科

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2019-11-05
1,要加入头文件 math.h
2,pow(x,y);//其作用是计算x的y次方。x、y及函数值都是double型
例:
我要计算2的5次方
源代码如下:
#include"stdio.h"
#include"math.h"
main()
{
long total;
int x = 2, y = 5;
total = pow(x,y); /*调用pow函数*/
printf("%ld",total);
getch();
}
第2个回答  2012-12-07
原型:extern float pow(float x, float y);

用法:#include <math.h>

功能:计算x的y次幂。

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

举例:

// pow.c

#include <syslib.h>

#include <math.h>

main()

{

clrscr(); // clear screen

textmode(0x00); // 6 lines per LCD screen

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

getchar();

return 0;

}
第3个回答  推荐于2017-10-13
原型:extern float pow(float x, float y);

用法:#include <math.h>

功能:计算x的y次幂。

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

举例:

// pow.c

#include <syslib.h>

#include <math.h>

main()

{

clrscr(); // clear screen

textmode(0x00); // 6 lines per LCD screen

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

getchar();

return 0;

}本回答被提问者采纳
第4个回答  2019-12-11
pow函数是C语言的一个库函数。
函数原型:double
pow(double
x,
double
y);


能:计算x^y


值:计算结果
举例如下:
double x = 3.14, y=2, z;
z = pow(x, y); // 计算x^y,即3.14的平方
注:使用pow函数的时候,需要将头文件#include<math.h>包含进源文件中。