-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlpi.c
More file actions
91 lines (49 loc) · 1.8 KB
/
mlpi.c
File metadata and controls
91 lines (49 loc) · 1.8 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
// Machin-like formula Pi/4 = 4 arctan(1/5) - arctan(1/239)
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// More info about this formula: https://mathworld.wolfram.com/Machin-LikeFormulas.html
// 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 mlpi mlpi.c -lmpfr
// Usage in command line:
// ./mlpi
#include <stdio.h>
#include <mpfr.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <time.h>
int mlpi(){
mpfr_t one, five, twothreenine, temp, pi;
mpfr_inits2(10000, one, five, twothreenine, temp, pi, NULL);
mpfr_set_ui (one, 1, MPFR_RNDD);
mpfr_set_ui (five, 5, MPFR_RNDD);
mpfr_set_ui (twothreenine, 239, MPFR_RNDD);
mpfr_div(temp,one,five, MPFR_RNDN);
mpfr_atan (temp, temp, MPFR_RNDN);
mpfr_mul_ui(temp,temp,4, MPFR_RNDN);
mpfr_set(pi, temp, MPFR_RNDN);
mpfr_div(temp,one,twothreenine, MPFR_RNDN);
mpfr_atan (temp, temp, MPFR_RNDN);
mpfr_sub(pi, pi, temp, MPFR_RNDN);
mpfr_mul_ui(pi, pi, 4, MPFR_RNDN);
//Print Out Answer
printf ("\n===================\nPI: ");
mpfr_out_str (stdout, 10, 0, pi, MPFR_RNDD);
printf ("\n===================\n\n");
mpfr_clears (one, five, twothreenine, temp, pi, NULL);
mpfr_free_cache ();
return 0;
}
int main(int argc, char * argv[]) {
time_t start; // system time var
time_t end; // system time var
time(&start); // Get the begining system time
mlpi(); // no iterations
time(&end); // Get the end system time
double dif;
dif = difftime (end,start); // calculate the diff
printf ("\nYour calculations took %.2lf seconds to run.\n", dif ); // time
return 0;
}