-
Notifications
You must be signed in to change notification settings - Fork 0
/
piarcsine.cpp
82 lines (60 loc) · 2.27 KB
/
piarcsine.cpp
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
/* CmSc 117 Exercise 3 (Extra Credit)
* Name: Gisselle Derije
* Date: 30 November 2020
*/
#include <iostream>
#include <iomanip>
#include <cmath>
#include "timer.hpp"
double PiArcSinForward(long N){
double s = 1;
double x = 0.125;
for(double k = 1; k <= N - 1; k++){
s = s + (x / ((2 * k) + 1));
x = x * ((0.125 * ((2 * k) + 1)) / (k + 1));
}
s = 3 * s;
return s;
}
double PiArcSinCompensated(long N){
double s = 1, x = 0.125, e = 0;
for(double k = 1; k <= N - 1; k++){
double y = (x / ((2 * k) + 1)) - e;
double t = s + y;
e = (t - s) - y;
s = t;
x = x * ((0.125 * ((2 * k) + 1)) / (k + 1));
}
s = 3 * s;
return s;
}
void printPiArcSin(double x){
std::cout << "Computed values using the power series of arc sine function:\n\n";
std::cout << "------------------------------------------------------------------------------" << std::endl;
std::cout << "N\tSUM\t\t\t\t\tRELATIVE ERROR\t\tCOMPENSATED SUM\t\tRELATIVE ERROR\n";
std::cout << "------------------------------------------------------------------------------" << std::endl;
timer stopwatch;
stopwatch.start();
for(int s = 1; s < 25; s++){
std::cout << s << "\t";
std::cout << std::fixed << std::setprecision(16) << PiArcSinForward(s) << "\t";
std::cout << std::scientific << std::setprecision(8) << std::abs(x - (PiArcSinForward(s))) / std::abs(x) << "\t\t";
std::cout << std::fixed << std::setprecision(16) << PiArcSinCompensated(s) << "\t";
std::cout << std::scientific << std::setprecision(8) << std::abs(x - (PiArcSinCompensated(s))) / std::abs(x) << std::endl;
}
std::cout << "------------------------------------------------------------------------------" << std::endl;
unsigned long sum = 0;
for (unsigned long k = 1; k < 10000001; k++) {
sum = k + sum;
}
stopwatch.stop();
std::cout << "\nElapsed time for approximating pi using arc sine: " << std::scientific << stopwatch.get_elapsed_time() << " seconds.\n" << std::endl;
}
void printPi(double x){
std::cout << std::fixed << std::setprecision(16) << "Machine value of pi:\n\t\t" << x << std::endl << std::endl;
}
int main() {
double x = M_PI;
printPi(x);
printPiArcSin(x);
}