-
Notifications
You must be signed in to change notification settings - Fork 0
/
perpetuity.cpp
102 lines (85 loc) · 2.74 KB
/
perpetuity.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <iomanip>
#include <cmath>
#include <chrono>
#include <stdio.h>
#include <Windows.h>
using namespace std;
#define PERCENTILE 100
enum inputs {
FIXED_AMOUNT = 1,
FIXED_RATE = 2,
};
class Perpetuity {
public:
double fixed_amount;
double fixed_interest_rate;
};
void printPerpetuityDetails(Perpetuity p) {
cout << setfill(' ') << fixed << setprecision(2);
cout << " =================================" << endl;
cout << " || PERPETUITY (IN CONTRACTS) ||" << endl;
cout << " =================================" << endl;
cout << " [INTEREST RATE IN PERCENTILE]" << endl;
cout << "\n";
cout << setw(28) << "FIXED AMOUNT" << setw(25) << "FIXED INTEREST RATE" << endl;
cout << setw(28) << p.fixed_amount << setw(25) << p.fixed_interest_rate << endl;
}
void closedFormPerpetuity(Perpetuity p) {
cout << "\nCLOSED FORM INFINITE PERPETUITY: " << (p.fixed_amount / p.fixed_interest_rate) << endl;
cout << "\n";
}
void infiniteFormPerpetuity(Perpetuity p, int iterations) {
auto begin = chrono::high_resolution_clock::now();
cout << "[OVER " << iterations << " ITERATIONS]: ";
double ifp = 0.0;
for(int i = 1; i <= iterations; i++) {
ifp += (p.fixed_amount / pow((1 + p.fixed_interest_rate), i));
}
cout << ifp << endl;
auto end = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cout << "EXECUTED IN: " << elapsed.count() * (1e-6) << " ms" << endl;
cout << "\n";
}
int main(int argc, char* argv[]) {
Perpetuity p;
p.fixed_amount = atof(argv[FIXED_AMOUNT]);
p.fixed_interest_rate = atof(argv[FIXED_RATE]);
p.fixed_interest_rate /= PERCENTILE;
printPerpetuityDetails(p);
closedFormPerpetuity(p);
cout << "INFINITE FORM PERPETUITY SIMULATIONS\n" << endl;
Sleep(500);
infiniteFormPerpetuity(p, 10);
Sleep(500);
infiniteFormPerpetuity(p, 15);
Sleep(500);
infiniteFormPerpetuity(p, 20);
Sleep(500);
infiniteFormPerpetuity(p, 25);
Sleep(500);
infiniteFormPerpetuity(p, 30);
Sleep(500);
infiniteFormPerpetuity(p, 35);
Sleep(500);
infiniteFormPerpetuity(p, 40);
Sleep(500);
infiniteFormPerpetuity(p, 45);
Sleep(500);
infiniteFormPerpetuity(p, 50);
Sleep(500);
infiniteFormPerpetuity(p, 75);
Sleep(500);
infiniteFormPerpetuity(p, 150);
Sleep(500);
infiniteFormPerpetuity(p, 300);
Sleep(500);
infiniteFormPerpetuity(p, 500);
Sleep(500);
infiniteFormPerpetuity(p, 1000);
Sleep(500);
infiniteFormPerpetuity(p, 10000);
Sleep(500);
cout << "We have reached the end..." << endl;
}