这段程序是根据这个求圆周率的公式得来的:
π/2=2/1·2/3·4/3·4/5·6/5·6/7·8/7·8/9···
代码如下:
- #include <stdio.h>
- int main(int argc,char *argv[])
- {
- double eps;
- double proc(double eps);
- printf("Please enter the accuracy:");
- scanf("%lf",&eps);
- printf("Pi = %lf",proc(eps));
- return 0;
- }
- double proc(double eps)
- {
- double s=0.0,t=1;
- int n=1;
- while(t>=eps)
- {
- s+=t;
- t=t*n/(2*n+1);
- n++;
- }
- return (2*s);
- }
一旦eps超出了0.0000001,计算出的π最大为3.141593
尝试修改第11行的代码:
- printf("Pi = %.16f",proc(eps));
可以得到的π=3.1415926535897922
转载于:https://blog.51cto.com/wenryxu/1172331