2020-12-15
程序员文章站
2024-01-29 10:29:34
...
pi的近似值计算
#include<stdio.h>
#include<math.h>
int main(void)
{
double pi, sum = 0, term, sign = 1.0;
int count = 0, n = 1;
do
{
term = sign / n ;
sum = sum+term ;
count++;
sign = -sign ;
n = n + 2 ;
} while( fabs(term) >= 1e-4);
pi = sum * 4 ;
printf("pi = %f\ncount = %d \n",pi,count) ;
return 0;
}
e的近似值计算
#include<stdio.h>
#include<math.h>
int main(void)
{
int n=1,count=1;
double sum=1,term=1;
while(fabs(term)>=1e-5)
{
term=term*(1.0/n);
sum=sum+term;
n++;
count++;
}
printf("sum=%f,count=%d\n",sum,count);
return 0;
}
上一篇: 2019/12/15错题
下一篇: 异步和单线程