为什么输出的π值会越来越小?
#include "stdafx.h"#include "stdio.h"
#include "math.h"
double pai(int n)
{
double mult1 = 1,mult2 = 2,sum = 0.5;
int index1,index2,index;
if(n==1)
;
else
for(index = 2;index <= n;index++)
{
for(index1 = 1;index1 <= (2 * index - 3);index1 += 2)
{
mult1 *= index1;
}
for(index2 = 1;index2 <= (2 * index - 2);index2 += 2)
{
mult2 *= index2;
}
sum += ((mult1/mult2/(2 * index - 1))*pow(0.5,2 * n - 1));
}
return sum*6;
}
int main(int argc, char* argv[])
{
int n;
int N=1;
printf("请输入项数:");
while(scanf("%d",&n)==0||n<=0)
{
printf("请输入大于零的整数!!!\n");
}
printf("项数N\t\tπ的估计值\n");
while(N<=n)
{
printf("%d\t\t%f\n",N,pai(N));
N++;
}
return 0;
}
w