为什么这调用函数里s1=s1*n/(2*n+1);变成 s1=s1*(n/(2*n+1));结果就变了?

为什么这调用函数里s1=s1*n/(2*n+1);变成 s1=s1*(n/(2*n+1));结果就变了?
#include
#include
double fun ( double eps)
{
double s=1.0,s1=1.0;
int n=1;
while(s1>=eps)
{
s1=s1*n/(2*n+1);
s=s+s1;
n++;
}
return(2*s);
}
main( )
{ double x;void NONO ();
printf("Input eps:") ;
scanf("%lf",&x); printf("\neps = %lf,PI=%lf\n",x,fun(x));
NONO();
}
void NONO ()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件.*/
FILE *fp,*wf ;
int i ;
double x ;
fp = fopen("in.dat","r") ;
wf = fopen("out.dat","w") ;
for(i = 0 ; i < 10 ; i++) {
fscanf(fp,"%lf",&x) ;
fprintf(wf,"%lf\n",fun(x)) ;
}
fclose(fp) ;
fclose(wf) ;
}
其他人气:651 ℃时间:2019-10-23 16:25:16
优质解答
s1=s1*n/(2*n+1);原因在于n设置的类型为int,不加括号时相当于s1=(s1*n)/(2*n+1);也就是s1=double/int所以结果自动转换为double型,就相当于是double/double,所以结果可以是小数.s1=s1*(n/(2*n+1));而(n/(2*n+1))加括...
我来回答
类似推荐
请使用1024x768 IE6.0或更高版本浏览器浏览本站点,以保证最佳阅读效果。本页提供作业小助手,一起搜作业以及作业好帮手最新版!
版权所有 CopyRight © 2012-2024 作业小助手 All Rights Reserved. 手机版