24 lines
445 B
C
24 lines
445 B
C
/* 用于精确计算 pi 值的算法 */
|
|
#include <stdio.h>
|
|
#include <math.h>
|
|
int main(void)
|
|
{
|
|
int sign = 1;
|
|
long int count = 0;
|
|
double pi = 0.0, n = 1.0, term = 1.0;
|
|
|
|
while (fabs(term) >= 1e-10)
|
|
{
|
|
pi = pi + term;
|
|
n = n + 2;
|
|
sign = -sign;
|
|
term = sign / n;
|
|
count++;
|
|
}
|
|
|
|
pi = pi * 4;
|
|
printf ("PI = %10.10f\n", pi);
|
|
printf ("Calculate count = %ld", count);
|
|
|
|
return 0;
|
|
} |