-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
223 lines (182 loc) · 6.21 KB
/
main.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <chrono>
#include <cmath>
using namespace std;
using namespace std::chrono;
float naive_sum(const float *array, int size);
float recursive_sum(float *array, int size);
float kahan_sum(float *array, int size);
int zadanie1_zadanie2();
float riemann_bottom_up(int n, float s);
float riemann_top_down(int n, float s);
float dirchlett_bottom_up(int n, float s);
float dirchlett_top_down(int n, float s);
double DP_riemann_bottom_up(int n, double s);
double DP_riemann_top_down(int n, double s);
double DP_dirchlett_bottom_up(int n, double s);
double DP_dirchlett_top_down(int n, double s);
int zadanie3();
int main() {
zadanie1_zadanie2();
zadanie3();
return 0;
}
int zadanie1_zadanie2() {
int size = 1e7;
float *T = new float[size];
float val = 0.53125;
float result = val * size;
for (int i = 0; i < 1e7; i++) {
T[i] = val;
}
auto start = high_resolution_clock::now();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
start = high_resolution_clock::now();
float sum_naive = naive_sum(T, 1e7);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
float error1 = abs(sum_naive - result);
float error2 = error1 / result;
cout << "blad bezwzgledny algorytmu naiwnego: " << error1 << endl;
cout << "blad wzgledny algorytmu naiwnego: " << error2 << endl;
cout << "czas wykonania algorytmu naiwnego: " << duration.count() << " microseconds" << endl;
cout << endl;
start = high_resolution_clock::now();
float sum_recursive = recursive_sum(T, 1e7);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
error1 = abs(sum_recursive - result);
error2 = error1 / result;
cout << "blad bezwzgledny algorytmu rekurencyjnego: " << error1 << endl;
cout << "blad wzgledny algorytmu rekurencyjnego: " << error2 << endl;
cout << "czas wykonania algorytmu rekurencyjnego: " << duration.count() << " microseconds" << endl;
cout << endl;
start = high_resolution_clock::now();
float sum_kahan = kahan_sum(T, 1e7);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
error1 = abs(sum_kahan - result);
error2 = error1 / result;
cout << "blad bezwzgledny algorytmu Kahana: " << error1 << endl;
cout << "blad wzgledny algorytmu Kahana: " << error2 << endl;
cout << "czas wykonania algorytmu Kahana: " << duration.count() << " microseconds" << endl;
delete[] T;
return 0;
}
float naive_sum(const float *array, int size) {
float sum = 0.0;
for (int i = 0; i < size; i++) {
sum += array[i];
}
return sum;
}
float rec(float *array, int start, int end){
if(start==end-1){
return array[start];
}
int middle = (start + end) / 2;
return rec(array, start, middle) + rec(array, middle, end);
}
float recursive_sum(float *array, int size) {
return rec(array,0,size);
}
float kahan_sum(float *array, int size){
float sum = 0.0f;
float err = 0.0f;
for(int i=0; i<size; i++){
float y = array[i] - err;
float temp = sum +y;
err = (temp - sum) - y;
sum = temp;
}
return sum;
}
int zadanie3(){
float T1[5] = {2.0, 3.6667, 5, 7.2, 10};
int T2[5] = {50,100,200,500,1000};
double DP_T1[5] = {2.0, 3.6667, 5, 7.2, 10};
for (int i = 0; i < 5; ++i) {
for (int n : T2) {
float s = T1[i];
double DP_s = DP_T1[i];
cout << "wartosc n: " << n << endl;
cout << "wartosc s: " << s << endl;
cout << endl;
// Single precision calculations
float riemann_bottom_up_result = riemann_bottom_up(n, s);
float riemann_top_down_result = riemann_top_down(n, s);
float dirchlett_bottom_up_result = dirchlett_bottom_up(n, s);
float dirchlett_top_down_result = dirchlett_top_down(n, s);
// Double precision calculations
double DP_riemann_bottom_up_result = DP_riemann_bottom_up(n, DP_s);
double DP_riemann_top_down_result = DP_riemann_top_down(n, DP_s);
double DP_dirchlett_bottom_up_result = DP_dirchlett_bottom_up(n, DP_s);
double DP_dirchlett_top_down_result = DP_dirchlett_top_down(n, DP_s);
// Print differences between bottom-up and top-down results
cout << "(DP) riemann difference: " << DP_riemann_bottom_up_result - DP_riemann_top_down_result << endl;
cout << "(DP) dirchlett difference: " << DP_dirchlett_bottom_up_result - DP_dirchlett_top_down_result << endl;
cout << endl;
cout << "(f) riemann difference: " << riemann_bottom_up_result - riemann_top_down_result << endl;
cout << "(f) dirchlett difference: " << dirchlett_bottom_up_result - dirchlett_top_down_result << endl;
cout << "--- --- --- ---" << endl;
}
}
return 0;
}
float riemann_bottom_up(int n, float s){
float sum =0.0;
for(int i=1; i<n;i++){
sum+= pow(i,-s);
}
return sum;
}
float riemann_top_down(int n, float s){
float sum =0.0f;
for(int i=n-1; i>=1;i--){
sum+= pow(i,-s);
}
return sum;
}
float dirchlett_bottom_up(int n, float s){
float sum =0.0f;
for(int i=1; i<n;i++){
sum+= pow(-1,i-1)*1/pow(i,s);
}
return sum;
}
float dirchlett_top_down(int n, float s){
float sum =0.0f;
for(int i=n-1; i>=1;i--){
sum+= pow(-1,i-1)*1/pow(i,s);
}
return sum;
}
double DP_riemann_bottom_up(int n, double s) {
double sum = 0.0;
for (int i = 1; i < n; i++) {
sum += 1 / pow(i, s);
}
return sum;
}
double DP_riemann_top_down(int n, double s) {
double sum = 0.0;
for (int i = n - 1; i >= 1; i--) {
sum += 1 / pow(i, s);
}
return sum;
}
double DP_dirchlett_bottom_up(int n, double s) {
double sum = 0.0;
for (int i = 1; i < n; i++) {
sum += pow(-1, i - 1) * 1 / pow(i, s);
}
return sum;
}
double DP_dirchlett_top_down(int n, double s) {
double sum = 0.0;
for (int i = n - 1; i >= 1; i--) {
sum += pow(-1, i - 1) * 1 / pow(i, s);
}
return sum;
}