用莱布尼茨公式求圆周率的C代码

  • 本文关键字:圆周率 代码 布尼茨 c
  • 更新时间 :
  • 英文 :


我被要求编写使用莱布尼茨公式查找数字pi的C代码。然而,结果应该是3.14,但结果是3.23。这是什么原因呢?

//Calculating the value of PI
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
//function main begins program execution
int main( void )
{
float pi = 0;
size_t k,n;
for ( n = 0 , k = 0; n <= 10 , k <= 10; n++ ,k++) {
pi +=  ( pow( -1, n ) * 4 )/ ( 2 * k + 1 );
}//end for
printf(" pi is %.2fn",pi );
getch();
return 0;
}//end main

我通过增加迭代来处理它。
基本上"还远远不够。

最新更新