forked from yafengabc/pimark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi.c
96 lines (93 loc) · 1.66 KB
/
pi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#define L 100000
#define N L/4+1
// L 为位数,N是array长度
void add(int *, int *, int*);
void sub(int *, int *, int*);
void div(int *, int, int*);
int main(void)
{
int s[N + 3] =
{
0
};
int w[N + 3] =
{
0
};
int v[N + 3] =
{
0
};
int q[N + 3] =
{
0
};
int n = (int)(L / 1.39793 + 1);
int k;
w[0] = 16 * 5;
v[0] = 4 * 239;
for (k = 1; k <= n; k++)
{
// 套用公式
div(w, 25, w);
div(v, 239, v);
div(v, 239, v);
sub(w, v, q);
div(q, 2 *k - 1, q);
if (k % 2)
// 奇数项
add(s, q, s);
else
// 偶数项
sub(s, q, s);
}
printf("%d.", s[0]);
for (k = 1; k < N; k++)
printf("%04d", s[k]);
printf("\n");
return 0;
}
void add(int *a, int *b, int *c)
{
int i, carry = 0;
for (i = N + 1; i >= 0; i--)
{
c[i] = a[i] + b[i] + carry;
if (c[i] < 10000)
carry = 0;
else
{
// 进位
c[i] = c[i] - 10000;
carry = 1;
}
}
}
void sub(int *a, int *b, int *c)
{
int i, borrow = 0;
for (i = N + 1; i >= 0; i--)
{
c[i] = a[i] - b[i] - borrow;
if (c[i] >= 0)
borrow = 0;
else
{
// 借位
c[i] = c[i] + 10000;
borrow = 1;
}
}
}
void div(int *a, int b, int *c)
{
// b 为除数
int i, tmp, remain = 0;
for (i = 0; i <= N + 1; i++)
{
tmp = a[i] + remain;
c[i] = tmp / b;
remain = (tmp % b) *10000;
}
}