-
Notifications
You must be signed in to change notification settings - Fork 0
/
piarctangent.cpp
84 lines (64 loc) · 2.35 KB
/
piarctangent.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
83
84
/* CmSc 117 Exercise 3 (Extra Credit)
* Name: Gisselle Derije
* Date: 30 November 2020
*/
#include <iostream>
#include <iomanip>
#include <cmath>
#include "timer.hpp"
double PiArcTanForward(long N) {
double s = 1;
for (double k = 1; k <= N - 1; k++) {
if (std::fmod(k, 2) == 0) {
s = s + (1 / ((2 * k) + 1));
}
else {
s = s - (1 / ((2 * k) + 1));
}
}
s = 4 * s;
return s;
}
double PiArcTanBackward(long N) {
double s = 0;
for (double k = N - 1; k > -1; k--) {
if (std::fmod(k, 2) == 0) {
s = s + (1 / ((2 * k) + 1));
}
else {
s = s - (1 / ((2 * k) + 1));
}
}
s = 4 * s;
return s;
}
void printPiArcTan(double x) {
std::cout << "Computed values using the power series of arc tangent function:\n\n";
std::cout << "-----------------------------------------------------------------------------------" << std::endl;
std::cout << "N\t\tFORWARD SUM\t\t\tRELATIVE ERROR\t\tBACKWARD SUM\t\tRELATIVE ERROR\n";
std::cout << "-----------------------------------------------------------------------------------" << std::endl;
timer stopwatch;
stopwatch.start();
for (int s = 0; s < 11; s++) {
std::cout << std::scientific << std::setprecision(0) << pow(10, s) << "\t";
std::cout << std::fixed << std::setprecision(16) << PiArcTanForward(pow(10, s)) << "\t";
std::cout << std::scientific << std::setprecision(8) << std::abs(x - PiArcTanForward(pow(10, s))) / std::abs(x) << "\t\t";
std::cout << std::fixed << std::setprecision(16) << PiArcTanBackward(pow(10, s)) << "\t";
std::cout << std::scientific << std::setprecision(8) << std::abs(x - PiArcTanBackward(pow(10, s))) / std::abs(x) << "\n";
}
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 tangent: " << 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);
printPiArcTan(x);
}