-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpill2.c
More file actions
190 lines (129 loc) · 4.02 KB
/
cpill2.c
File metadata and controls
190 lines (129 loc) · 4.02 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Madhava - Leibniz, Gottfried Wilhelm Leibniz iterative algorithm for Pi approximation.
// CPILL2 - COMPUTE PI LONG LEIBNIZ V2
//
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// This is free code to calculate pi to an arbitrary degree of precision.
// There is no warranty or guarantee of any kind.
// The mpfr library has further restrictions.
// To Compile:
// gcc -o cpill2 cpill2.c -lmpfr
// Usage:
// ./cpill2 1000 1000
#include <stdio.h>
#include <mpfr.h> // for floating point mumbers
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#ifdef __APPLE__
#include <mach/mach_time.h>
#else
#include <time.h>
#endif
int cpill2(char *stop, unsigned int *bt)
{
mpfr_t r, i, n, one, two, div, sum, pi;
mpfr_inits2(*bt, r, i, n, one, two, div, sum, pi, NULL);
mpfr_set_str (n, stop, 10, MPFR_RNDD);
mpfr_sub_ui(n, n, 1, MPFR_RNDD); // i - 1 as end, example 99 from 100
mpfr_set_si (i, 0, MPFR_RNDD);
mpfr_set_d (one, 1.0, MPFR_RNDD);
mpfr_set_si (two, 2, MPFR_RNDD);
mpfr_set_si (div, 0, MPFR_RNDD);
mpfr_set_si (sum, 0, MPFR_RNDD);
/* Applying Classic Leibniz Formula V2 */
/*
for (i=0; i<(iters-1); i++)
{
if (i % 2 == 0) {
pi_4 += 1.0 / (1.0 + 2 * i);
} else {
pi_4 -= 1.0 / (1.0 + 2 * i);
}
/// count++;
}
pi = 4.0 * pi_4;
*/
while(mpfr_cmpabs(i,n)<0)
{
mpfr_fmod (r, i, two, MPFR_RNDD);
if(mpfr_cmpabs_ui(r,0)==0)
{
// even
mpfr_mul(div, two, i, MPFR_RNDD);
mpfr_add_si(div,div,1,MPFR_RNDD);
mpfr_div(div,one,div,MPFR_RNDD);
mpfr_add(sum,sum,div,MPFR_RNDD);
}
else
{
// odd
mpfr_mul(div, two, i, MPFR_RNDD);
mpfr_add_si(div,div,1,MPFR_RNDD);
mpfr_div(div,one,div,MPFR_RNDD);
mpfr_sub(sum,sum,div,MPFR_RNDD);
}
mpfr_add_si(i, i, 1, MPFR_RNDD);
}
// print out last - 1 result
mpfr_mul_si(pi,sum,4,MPFR_RNDD);
mpfr_printf ("\n===================\nPI for (n = %.*RZf): ",0,i);
mpfr_out_str (stdout, 10, *bt, pi, MPFR_RNDD);
printf ("\n===================\n\n");
// print out last result
mpfr_add_si(i, i, 1, MPFR_RNDD);
mpfr_fmod (r, i, two, MPFR_RNDD);
if(mpfr_cmpabs_ui(r,0)==0)
{
// even
mpfr_mul(div, two, i, MPFR_RNDD);
mpfr_add_si(div,div,1,MPFR_RNDD);
mpfr_div(div,one,div,MPFR_RNDD);
mpfr_add(sum,sum,div,MPFR_RNDD);
}
else
{
// odd
mpfr_mul(div, two, i, MPFR_RNDD);
mpfr_add_si(div,div,1,MPFR_RNDD);
mpfr_div(div,one,div,MPFR_RNDD);
mpfr_sub(sum,sum,div,MPFR_RNDD);
}
mpfr_mul_si(pi,sum,4,MPFR_RNDD);
mpfr_printf ("\n===================\nPI for (n = %.*RZf): ",0,i);
mpfr_out_str (stdout, 10, *bt, pi, MPFR_RNDD);
printf ("\n===================\n\n");
return 0;
}
int main(int argc, char * argv[]){
unsigned int b;
char * i;
if (argc <= 2){
printf ("Usage: %s <number of iterations> <number of bits>\n", argv[0]);
return 1;
}
i = argv[1];
b = atoi(argv[2]);
assert(i != NULL);
assert( b >= 1);
// Get system time START
#ifdef __APPLE__
uint64_t start = mach_absolute_time();
#else
clock_t start = clock();
#endif
// Run the main procedure.
cpill2(i,&b);
// Get system time END
#ifdef __APPLE__
uint64_t end = mach_absolute_time();
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
long double diff = (end - start) * timebase_info.numer / timebase_info.denom; // nano sec
printf("Your calculations took %.3Lf seconds to run.\n", diff / 1e9 );
#else
clock_t end = clock();
printf("Your calculations took %.3Lf seconds to run.\n", (long double)(end - start) / CLOCKS_PER_SEC );
#endif
mpfr_free_cache ();
return 0;
}