This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCalcOperations.cpp
245 lines (195 loc) · 7.3 KB
/
CalcOperations.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
#ifndef CALCULATOR_CALCOPERATIONS_CPP
#define CALCULATOR_CALCOPERATIONS_CPP
#include "CalcOperations.hpp"
#include "Calculator.hpp"
#include "CalcASTElem.hpp"
#include "CalcASTEnum.hpp"
#include "CalcASTException.hpp"
#include "boilerplate/ld_boilerplate.hpp"
template <class Num>
Num CalcOperations<Num>::Binary::exponentiation(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
CalcASTElem lhs = src.children[0];
CalcASTElem rhs = src.children[1];
Num rhs_final = calc->execute_ast(rhs, validate_only);
/**
* Easy way out, just return 1. Anything to the power of 0 is
* always 1.
*/
if (rhs_final == 0) {
if (validate_only) {
calc->execute_ast(lhs, validate_only);
}
return 1;
}
/**
* Fractional exponents may not be rational, and our
* arbitrary-precision types can only represent rational numbers
*/
if (!calc->is_int(rhs_final) || rhs_final < 0) {
throw CalcASTException(calc->get_token(rhs),
L"Powers must be a positive integer");
}
if (validate_only) {
return calc->execute_ast(lhs, validate_only);
}
Num lhs_final = calc->execute_ast(lhs);
return math::Rational(
LD::ipow(lhs_final.numerator(), rhs_final.numerator()),
LD::ipow(lhs_final.denominator(), rhs_final.numerator()));
}
template <class Num>
Num CalcOperations<Num>::Binary::multiplication(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
CalcASTElem lhs = src.children[0];
CalcASTElem rhs = src.children[1];
if (validate_only) {
calc->execute_ast(lhs, validate_only);
return calc->execute_ast(rhs, validate_only);
}
return calc->execute_ast(lhs) * calc->execute_ast(rhs);
}
template <class Num>
Num CalcOperations<Num>::Binary::division(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
CalcASTElem lhs = src.children[0];
CalcASTElem rhs = src.children[1];
Num rhs_final = calc->execute_ast(rhs, validate_only);
if (rhs_final == 0) {
throw CalcASTException(calc->get_token(src.children[1]),
L"Can't divide by 0");
}
if (validate_only) {
return calc->execute_ast(lhs, validate_only);
}
return calc->execute_ast(lhs) / rhs_final;
}
template <class Num>
Num CalcOperations<Num>::Binary::addition(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
CalcASTElem lhs = src.children[0];
CalcASTElem rhs = src.children[1];
if (validate_only) {
calc->execute_ast(lhs, validate_only);
return calc->execute_ast(rhs, validate_only);
}
return calc->execute_ast(lhs) + calc->execute_ast(rhs);
}
template <class Num>
Num CalcOperations<Num>::Binary::subtraction(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
CalcASTElem lhs = src.children[0];
CalcASTElem rhs = src.children[1];
if (validate_only) {
calc->execute_ast(lhs, validate_only);
return calc->execute_ast(rhs, validate_only);
}
return calc->execute_ast(lhs) - calc->execute_ast(rhs);
}
template <class Num>
Num CalcOperations<Num>::Binary::assignment(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
CalcASTElem lhs = src.children[0];
CalcASTElem rhs = src.children[1];
/**
* 3 = 3.141592653589793
*
* I don't think so
*/
if (lhs.token.type != TK_VARIABLE) {
throw CalcASTException(calc->get_token(lhs),
L"Can't assign to non-variable");
} else if (lhs.token.data == L"_") {
throw CalcASTException(calc->get_token(lhs),
L"Assignment will be overwritten");
}
if (validate_only) return calc->execute_ast(rhs, validate_only);
return calc->variables[lhs.token.data] = calc->execute_ast(rhs);
}
template <class Num>
Num CalcOperations<Num>::Unary::factorial(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
CalcASTElem num = src.children[0];
Num num_final = calc->execute_ast(num, validate_only);
if (!calc->is_int(num_final)) {
throw CalcASTException(calc->get_token(num),
L"Can't calculate factorial of non-integer");
} else if (num_final < 0) {
throw CalcASTException(calc->get_token(num),
L"Can't calculate factorial of negative"
L" integer");
}
if (validate_only) return 1;
Num factored = 1;
for (Num i(1); i <= num_final; i++) {
factored *= i;
}
return factored;
}
template <class Num>
Num CalcOperations<Num>::Unary::dbl_factorial(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
CalcASTElem num = src.children[0];
Num num_final = calc->execute_ast(num, validate_only);
if (!calc->is_int(num_final)) {
throw CalcASTException(calc->get_token(num),
L"Can't calculate double factorial of non-"
L"integer");
} else if (num_final < -1) {
throw CalcASTException(calc->get_token(num),
L"Can't calculate double factorial of"
L" integer below -1");
}
if (validate_only) return 1;
Num factored = 1;
for (Num i(1); i <= num_final; i++) {
if (i.numerator() % 2 == num_final.numerator() % 2) {
factored *= i;
}
}
return factored;
}
template <class Num>
Num CalcOperations<Num>::Unary::super_factorial(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
CalcASTElem num = src.children[0];
Num num_final = calc->execute_ast(num, validate_only);
if (!calc->is_int(num_final)) {
throw CalcASTException(calc->get_token(num),
L"Can't calculate super factorial of non-"
L"integer");
} else if (num_final < 0) {
throw CalcASTException(calc->get_token(num),
L"Can't calculate super factorial of"
L" negative integer");
}
if (validate_only) return 1;
Num factored = 1;
Num factorial = 1;
for (Num i(1); i <= num_final; i++) {
factorial *= i;
factored *= factorial;
}
return factored;
}
template <class Num>
Num CalcOperations<Num>::Unary::negation(Calculator<Num> * calc,
CalcASTElem src,
bool validate_only) {
return -calc->execute_ast(src.children[0], validate_only);
}
template <class Num>
Num CalcOperations<Num>::Unary::plus(Calculator<Num> * calc,
CalcASTElem src, bool validate_only) {
return calc->execute_ast(src.children[0], validate_only);
}
#endif //CALCULATOR_CALCOPERATIONS_CPP