生无涯

吾生也有涯,而知也无涯,以无涯随有涯,乐以忘忧,生亦无涯矣www.cnblogs.com/shengwuya
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

a program a day 17 (math,calculate PI)

Posted on 2010-10-13 23:35  生无涯  阅读(180)  评论(0编辑  收藏  举报

/**
*求PI,用正多边形逼近法、数值概率算法
**/
#include<stdio.h>
#include<math.h>
double getPI(int n);
int main()
{
 int n = 1;
 double PI;
 printf("please input the accuracy:\n");
 scanf("%d",&n);
 PI = getPI(n);
 printf("the similar value of PI is :\n%f\n",PI);
 getchar();
 return 0;
}
double getPI(int n)
{
 int i = 4;
 double b = sqrt(2)/2.0;
 for(int div = 0;div < n;div++)
 {
  b = sqrt(2.0 - 2.0 * sqrt(1.0-b*b))*0.5;
  i *= 2;
 } 
 return b * i;
}

//(to be continued)