-
Notifications
You must be signed in to change notification settings - Fork 1
/
PiCalculator.cpp
249 lines (239 loc) · 6.86 KB
/
PiCalculator.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// PiCalculator.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <string>
#include <thread>
#define thread_count 6
#define divide_precision 100
using namespace std;
string thread_result[thread_count];
string strDiv(int a, int b, int precision) {
int result, remainder;
result = a / b;
string ans = "";
int tmp = result;
string temp = "";
if (result == 0) {
ans.push_back('0');
}
else {
while (tmp != 0) {
temp = "";
temp.push_back('0' + (tmp % 10));
//ans.push_back('0' + (tmp % 10));
ans = temp + ans;
tmp /= 10;
}
}
//ans.push_back(result);
ans.push_back('.');
a = a - result * b;
a *= 10;
while (precision > 0) {
result = a / b;
remainder = a - result * b;
if (remainder == 0) {
ans.push_back('0' + result);
break;
}
else {
ans.push_back('0' + result);
}
a = remainder * 10;
precision--;
}
return ans;
}
/* precondition: a is larger than b */
string strMinus(string a, string b) {
string::size_type dotLoca = a.find(".", 0);
string::size_type dotLocb = b.find(".", 0);
string fraca = a.substr(dotLoca + 1);
string fracb = b.substr(dotLocb + 1);
string inta = a.substr(0, dotLoca);
string intb = b.substr(0, dotLocb);
while (fraca.size() < fracb.size()) {
fraca = fraca + "0";
}
/*
if (inta.size() < intb.size()) {
string tmp = inta;
inta = intb;
intb = tmp;
}*/
int lena = fraca.size();
int lenb = fracb.size();
int len_inta = inta.size();
int len_intb = intb.size();
//int ptr = lena - 1;
int underflow = 0;
string result = "";
int addResult = 0;
int toAdd1;
int toAdd2;
string tmp;
for (int ptr = lena - 1; ptr >= 0; ptr--) {
toAdd1 = fraca[ptr] - '0';
if (ptr >= lenb) {
toAdd2 = 0;
}
else {
toAdd2 = fracb[ptr] - '0';
}
addResult = toAdd1 - toAdd2 + underflow;
if (addResult < 0) {
underflow = -1;
addResult = addResult + 10;
}
else {
underflow = 0;
}
tmp = "";
tmp.push_back('0' + addResult);
result = tmp + result;
}
result = "." + result;
for (int ptr = len_inta - 1; ptr >= 0; ptr--) {
toAdd1 = inta[ptr] - '0';
if (ptr - (len_inta - len_intb) < 0) {
toAdd2 = 0;
}
else {
toAdd2 = intb[ptr - (len_inta - len_intb)] - '0';
}
addResult = toAdd1 - toAdd2 + underflow;
if (addResult < 0) {
underflow = -1;
addResult = addResult + 10;
}
else {
underflow = 0;
}
tmp = "";
tmp.push_back('0' + addResult);
result = tmp + result;
}
/*if (overflow == 1) {
result = "1" + result;
}*/
return result;
}
string strAdd(string a, string b) {
string::size_type dotLoca = a.find(".", 0);
string::size_type dotLocb = b.find(".", 0);
string fraca = a.substr(dotLoca + 1);
string fracb = b.substr(dotLocb + 1);
string inta = a.substr(0, dotLoca);
string intb = b.substr(0, dotLocb);
if (fraca.size() < fracb.size()) {
string tmp = fraca;
fraca = fracb;
fracb = tmp;
}
if (inta.size() < intb.size()) {
string tmp = inta;
inta = intb;
intb = tmp;
}
int lena = fraca.size();
int lenb = fracb.size();
int len_inta = inta.size();
int len_intb = intb.size();
//int ptr = lena - 1;
int overflow = 0;
string result = "";
int addResult = 0;
int toAdd1;
int toAdd2;
string tmp;
for (int ptr = lena - 1; ptr >= 0; ptr--) {
toAdd1 = fraca[ptr] - '0';
if (ptr >= lenb) {
toAdd2 = 0;
}
else {
toAdd2 = fracb[ptr] - '0';
}
addResult = toAdd1 + toAdd2 + overflow;
if (addResult >= 10) {
overflow = 1;
}
else {
overflow = 0;
}
addResult = addResult % 10;
tmp = "";
tmp.push_back('0' + addResult);
result = tmp + result;
}
result = "." + result;
for (int ptr = len_inta - 1; ptr >= 0; ptr--) {
toAdd1 = inta[ptr] - '0';
if (ptr - (len_inta - len_intb) < 0) {
toAdd2 = 0;
} else {
toAdd2 = intb[ptr - (len_inta-len_intb)] - '0';
}
addResult = toAdd1 + toAdd2 + overflow;
if (addResult >= 10) {
overflow = 1;
}
else {
overflow = 0;
}
addResult = addResult % 10;
tmp = "";
tmp.push_back('0' + addResult);
result = tmp + result;
}
if (overflow == 1) {
result = "1" + result;
}
return result;
}
void calculate_pi(int thread_id, int start_offset, int n) {
thread_result[thread_id] = "1.0";
for (int i = start_offset; i < n; i++) {
if (i % 1000 == 0) {
cout << "%" << (i-start_offset) * 100.0 / (n-start_offset) << "\n";
}
if (i % 2 == 1) {
thread_result[thread_id] = strMinus(thread_result[thread_id], strDiv(1, (2 * i + 1), divide_precision));
}
else {
thread_result[thread_id] = strAdd(thread_result[thread_id], strDiv(1, (2 * i + 1), divide_precision));
}
}
}
int main()
{
int n;
std::cout << "Please give a loop count: ";
std::cin >> n;
string result = "1.0";
thread worker_threads[thread_count];
int sign;
n = (n / thread_count) * thread_count + 1;
int work_load = (n - 1) / thread_count;
for (int i = 0; i < thread_count; i++) {
worker_threads[i] = thread(calculate_pi, i, 1 + i * work_load, 1 + (1+ i) * work_load);
}
for (int i = 0; i < thread_count; i++) {
worker_threads[i].join();
}
for (int i = 0; i < thread_count; i++) {
result = strAdd(result, thread_result[i]);
}
float thread_count_float = thread_count;
result = strMinus(result, to_string(thread_count_float));
cout << "The value of pi/4 is " << result;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file