求PI的近似值,用公式PI/4=1-1/3+1/5-1/7+...
1#include <stdio.h>
2#include <math.h>
3#include <conio.h>
4void main()
5{
6 clrscr();
7 int f=1;
8 float pi=0,t=1,v=1;
9 while (fabs(t)>1e-6)
10 {
11 pi=pi+t;
12 v+=2; /*准备下一个累加的数据*/
13 f=-f;
14 t=f/v;
15 }
16 pi*=4;
17 printf("\n pi=%10.8f",pi);
18}
2#include <math.h>
3#include <conio.h>
4void main()
5{
6 clrscr();
7 int f=1;
8 float pi=0,t=1,v=1;
9 while (fabs(t)>1e-6)
10 {
11 pi=pi+t;
12 v+=2; /*准备下一个累加的数据*/
13 f=-f;
14 t=f/v;
15 }
16 pi*=4;
17 printf("\n pi=%10.8f",pi);
18}