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 pathRationalCalculator.cpp
603 lines (505 loc) · 18 KB
/
RationalCalculator.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#ifndef CALCULATOR_RATIONALCALCULATOR_CPP
#define CALCULATOR_RATIONALCALCULATOR_CPP
#include <vector>
#include <string>
#include <valarray>
#include "RationalCalculator.hpp"
#include "Calculator.cpp"
#include "boilerplate/precision/math_Rational.h"
#include "Irrational.hpp"
bool RationalCalculator::is_int(const math::Rational & num) {
return num.denominator().abs() == 1;
}
std::wstring RationalCalculator::commatize_str(const std::wstring & str) {
if (str.length() <= 3) {
return str;
}
unsigned long rem = str.length() % 3;
std::wstring built = str.substr(0, rem);
for (unsigned long i = rem; i < str.length(); i += 3) {
built += L"," + str.substr(i, 3);
}
return built.substr(static_cast<unsigned long>(rem == 0));
}
std::wstring RationalCalculator::to_string(const math::Rational & num) {
if (precision == -2) {
if (num.denominator() == 1) {
return commatize_str(LD::s2wstr(num.to_string()));
}
return commatize_str(LD::s2wstr(num.numerator().to_string())) + L"/" +
commatize_str(LD::s2wstr(num.denominator().to_string()));
}
std::wstring decimal;
if (precision < 0) {
decimal = LD::s2wstr(num.to_precise_string());
} else {
decimal = LD::s2wstr(num.to_string(static_cast<size_t>(precision)));
}
if (commatize) {
unsigned long after_sign = decimal[0] == '-' ? 1 : 0;
unsigned long radix_pos = decimal.find_first_of('.');
/**
* there is only an integer portion (and possibly sign) - no decimal
*/
if (radix_pos == std::wstring::npos) {
/**
* Sign + commatized integer portion
*/
return decimal.substr(0, after_sign) +
commatize_str(decimal.substr(after_sign));
}
/**
* The sign + the integer part + the radix point + the decimal part
*/
return decimal.substr(0, after_sign) +
commatize_str(decimal.substr(after_sign, radix_pos -
after_sign)) +
L"." + decimal.substr(radix_pos + 1);
}
return decimal;
}
RationalCalculator::RationalCalculator() : Calculator<math::Rational>() {
register_commands();
generate_commands_help();
register_functions();
generate_functions_help();
}
void RationalCalculator::register_commands() {
/**
* document function added manually in main.cpp
*/
help_pages[L":exit"] = L":exit exits the calculator. That's it, really.\n"
L"Usage: :exit";
help_pages[L":prec"] =
L":prec - set the precision of displayed numbers.\n"
L"Usage: :prec <#/off>\n"
L"Example: :prec 20 - displays 20 digits of precision\n"
L"Example: :prec off - displays results to 'infinite' precision -"
L" detects repeating decimals. For examplen 1/6 == 0.1(6)\n\n"
L"Example: :prec frac - displays results using fractions (e.g. 1/3)\n\n"
L"This only affects the output. Only the :irrational commands require"
L" this to be some number of digits.\n"
L"# must be a positive integer. It may be zero.";
commands[L"prec"] =
[this](std::vector<Token> args,
bool validate_only = false) -> std::wstring {
if (args.size() == 2) {
Token proposed = args[1];
/**
* Throw an exception early for validation purposes
*/
if (proposed.data != L"off" && proposed.data != L"frac" &&
proposed.data.find_first_not_of(L"0123456789") !=
std::wstring::npos) {
throw CalcASTException(proposed,
L"Not a positive integer, `off` or"
L" `frac`");
}
/**
* Only actually execute if we're not only validating
*/
if (!validate_only) {
/**
* Set precision to a negative number to signal we want
* fractions instead
*/
if (proposed.data == L"off") {
precision = -1;
} else if (proposed.data == L"frac") {
precision = -2;
} else {
/**
* Try to shove the input into `precision`. If it fails,
* complain loudly
*/
try {
std::wstringstream ss(proposed.data);
ss >> precision;
} catch (std::exception &) {
throw CalcASTException(proposed,
L"Can't convert to integer");
}
}
}
return L"Precision set!";
}
throw CalcASTException(args[0], L"Expected 1 argument, got " +
LD::wtostring(args.size() - 1));
};
help_pages[L":debug"] =
L"Various debug utilities for looking at the calculator's internal,"
L" intermediate states before a result has finished being"
L" evaluated.\n"
L"FORCING something causes it to be printed as soon as it's"
L" available. Even if the command didn't finish completely, even if"
L" it fails halfway through, or even if the result is thrown away,"
L" debug information will still be printed.\n\n"
L"Usage: :debug [tokens/ast] <on/off/force>\n"
L"Example: :debug ast on - turns on AST printing\n"
L"Example: :debug ast force - FORCES AST printing\n"
L"Example: :debug on - turns on ALL printing (works with 'off' and"
L" 'force' too)";
commands[L"debug"] =
[this](const std::vector<Token> args,
bool validate_only = false) -> std::wstring {
/**
* if there are 2 arguments, the user specified either tokens or ast
* the command name is included in args
*/
if (args.size() == 2 || args.size() == 3) {
Token group = args[0],
setting = args[1];
if (args.size() == 3) {
group = args[1];
setting = args[2];
}
/**
* Validate early
*/
if (args.size() == 3 && group.data != L"tokens" &&
group.data != L"ast") {
throw CalcASTException(group,
L"Must be either `tokens` or"
L" `ast`");
}
if (setting.data != L"on" && setting.data != L"off" &&
setting.data != L"force") {
throw CalcASTException(setting, L"Setting must be `on`,"
L" `off`, or `force`");
}
if (!validate_only) {
bool set_on = setting.data == L"on",
force = setting.data == L"force";
if (args.size() == 2) {
tk_debug_force = force;
tk_debug = set_on;
ast_debug_force = force;
ast_debug = set_on;
} else {
bool is_tokens = group.data == L"tokens",
* target = & (
is_tokens ? tk_debug : ast_debug
),
* force_target = & (
is_tokens ? tk_debug_force : ast_debug_force
);
/**
* `set_on` doesn't matter if `force` is `true`
*/
* force_target = force;
* target = set_on;
}
}
return L"Debug state successfully set!";
}
throw CalcASTException(args[0],
L"Expected 1 or 2 argument, got " +
LD::wtostring(args.size() - 1));
};
help_pages[L":vars"] = L":vars prints all variables you have set.\n"
L"Usage: :vars";
commands[L"vars"] =
[this](const std::vector<Token> & args,
bool validate_only = false) -> std::wstring {
if (args.size() != 1) {
throw CalcASTException(args[0],
L"Expected no arguments, got " +
LD::wtostring(args.size() - 1));
}
std::wstring built;
/**
* display _, or display that there is no last result
*/
try {
built = L"_: " + to_string(variables.at(L"_"));
} catch (std::out_of_range &) {
built = L"_: no last result";
}
/**
* add every variable except for _ (already accounted for) to the
* string, using built-in to_string for each one
*/
for (auto & variable : variables) {
if (variable.first != L"_") {
built.append(L"\n" + variable.first +
L": " + to_string(variable.second));
}
}
return built;
};
help_pages[L":clear"] = L":clear resets the calculator, removing all"
L" variables.\n"
L"Usage: :clear";
commands[L"clear"] =
[this](const std::vector<Token> & args,
bool validate_only = false) -> std::wstring {
if (args.size() != 1) {
throw CalcASTException(args[0],
L"Expected no arguments, got " +
LD::wtostring(args.size() - 1));
}
if (!validate_only) {
variables.clear();
}
return L"Variables successfully cleared!";
};
help_pages[L":delvar"] = L":delvar deletes a variable.\n"
L"Usage: :delvar <variable>\n"
L"Example: :delvar x - removes the variable `x`";
commands[L"delvar"] =
[this](const std::vector<Token> & args,
bool validate_only = false) -> std::wstring {
if (args.size() != 2) {
throw CalcASTException(args[0],
L"Expected 1 argument, got " +
LD::wtostring(args.size() - 1));
}
Token to_remove = args[1];
try {
variables.at(to_remove.data);
if (!validate_only) {
variables.erase(to_remove.data);
}
return L"Variable successfully removed!";
} catch (std::out_of_range &) {
throw CalcASTException(to_remove,
L"No such variable exists");
}
};
help_pages[L":commas"] = L":commas sets whether or not to show numbers with"
L" thousands separators.\n"
L"Usage: :commas <on/off>\n"
L"Example: :commas off - hides thousands"
L" separators";
commands[L"commas"] =
[this](const std::vector<Token> & args,
bool validate_only = false) -> std::wstring {
/**
* if there isn't exactly 1 argument or it isn't on/off, display
* help
*/
if (args.size() != 2) {
throw CalcASTException(args[0],
L"Expected 1 argument, got " +
LD::wtostring(args.size() - 1));
}
Token setting = args[1];
if (setting.data != L"on" && setting.data != L"off") {
throw CalcASTException(setting, L"Must be `on` or `off`");
}
if (!validate_only) {
commatize = setting.data == L"on";
}
return L"Thousands separators turned " + setting.data + L"!";
};
help_pages[L":irrational"] =
L"Various utilities related to irrational numbers.\n\n"
L"Usage: :irrational <pi/e/golden> <var> - puts pi, e, or the golden"
L" ratio into `var`. Uses the current precision as set by :prec.";
commands[L"irrational"] =
[this](const std::vector<Token> & args, bool validate_only = false)
-> std::wstring {
if (args.size() != 3) {
throw CalcASTException(args[0],
L"Expected 2 arguments, got " +
LD::wtostring(args.size() - 1));
}
Token subcommand = args[1];
if (subcommand.data != L"pi" && subcommand.data != L"e" &&
subcommand.data != L"golden") {
throw CalcASTException(subcommand,
L"Invalid subcommand (pi/e/golden)");
}
Token variable = args[2];
if (!CONFORMS(variable.data,
CalcTokenizer<math::Rational>::var_chars)) {
throw CalcASTException(variable, L"Invalid variable name");
}
if (precision < 0) {
throw CalcASTException(args[0],
L"Can't calculate perfectly (precision"
L" must be set, see :help :prec)");
}
if (!validate_only) {
try {
if (subcommand.data == L"pi") {
variables[variable.data] = Irrational::pi(precision);
} else if (subcommand.data == L"e") {
variables[variable.data] = Irrational::e(
static_cast<size_t>(precision));
} else if (subcommand.data == L"golden") {
variables[variable.data] = Irrational::golden_ratio(
static_cast<size_t>(precision));
} else if (subcommand.data == L"sqrt") {
variables[variable.data] = Irrational::sqrt(
static_cast<size_t>(precision), variables.at(L"_"));
}
} catch (std::exception & e) {
throw CalcASTException(args[1],
LD::s2wstr(std::string(e.what())));
}
}
return L"Success!";
};
help_pages[L":sort"] =
L":sort sorts a sequence of numbers in ascending order.\n"
L"Example: :sort 3 8 1 6 -> 1, 3, 6, 8";
commands[L"sort"] =
[this](const std::vector<Token> & args, bool validate_only = false)
-> std::wstring {
std::vector<math::Rational> sorted;
std::vector<Token> args2 = args;
args2.erase(args2.begin());
for (const Token & tk : args2) {
try {
sorted.emplace_back(LD::w2str(tk.data));
} catch (std::exception & e) {
throw CalcASTException(tk,
L"Unable to convert to number: " +
LD::s2wstr(e.what()));
}
}
std::sort(sorted.begin(), sorted.end());
std::wstring built;
for (math::Rational & num : sorted) {
//built.append(to_string(num) + L", ");
built.append(LD::s2wstr(num.to_string()) + L", ");
}
return built.substr(0, built.length() - 2);
};
}
void RationalCalculator::generate_commands_help() {
std::wstring cmds_help =
L"This calculator has a few extra commands (besides :help)"
L" that can either affect how the calculator behaves or"
L" provide convenience. Run :help :<command> to see"
L" command information. For example, :help :prec will show"
L" help on the :prec command. Here's a list of"
L" commands:\n\n";
for (auto & page : help_pages) {
if (page.first[0] == ':') {
cmds_help.append(page.first + L", ");
}
}
help_pages[L"commands"] = cmds_help.substr(0, cmds_help.length() - 2);
help_pages.at(L"starthere").append(L"\n:help commands");
}
void RationalCalculator::register_functions() {
help_pages[L"sqrt()"] =
L"sqrt() calculates the square root of a value.\n"
L"Usage: sqrt(<value>)\n"
L"Example: sqrt(4) - calculates the square root of 4. Returns 2.\n"
L"Example: sqrt(2) - calculates the square root of 2. Returns"
L" approximately 1.4142135623730950488.";
functions[L"sqrt"].insert(
// @formatter:off
std::make_pair<unsigned, CalcOpFunc(math::Rational)>(
// @formatter:on
1,
[](Calculator<math::Rational> * calc, CalcASTElem src,
bool validate_only = false) -> math::Rational {
auto * rcalc = reinterpret_cast<RationalCalculator *>(calc);
if (rcalc->precision < 0) {
throw CalcASTException(src.token,
L"Can't calculate perfectly"
L" (precision must be set, see :help"
L" :prec)");
}
if (!validate_only) {
try {
return Irrational::sqrt(
static_cast<size_t>(rcalc->precision),
calc->execute_ast(src.children[0]));
} catch (std::exception & e) {
throw CalcASTException(calc->get_token(src.children[0]),
LD::s2wstr(
std::string(e.what())));
}
} else {
return rcalc->execute_ast(src.children[0], validate_only);
}
}
));
help_pages[L"mean()"] =
L"mean() calculates the mean of a set of elements.\n"
L"Usage: mean(<element,...>)\n"
L"Example: mean(10, 20) - calculates the mean of 10 and 20. Returns"
L" 15.";
variadic_funcs[L"mean"] =
[](Calculator<math::Rational> * calc, CalcASTElem src,
bool validate_only = false) -> math::Rational {
std::valarray<math::Rational> elems(math::Rational(0),
src.children.size());
for (int i = 0; i < src.children.size(); i++) {
elems[i] = calc
->execute_ast(src.children[i], validate_only);
}
return elems.sum() / static_cast<int>(elems.size());
};
help_pages[L"stdvar()"] =
L"stdvar() calculates the standard variance of a set of elements.\n"
L"Usage: stdvar(<sample/population>, <element,...>)\n"
L"Example: sqrt(stdvar(population, 10, 2, 38, 23, 38, 23, 21)) -"
L" calculates the standard deviation (sqrt of variance) of the"
L" population {10, 2, 38, 23, 38, 23, 21}. Returns approximately"
L" 12.29899614287479072189.";
variadic_funcs[L"stdvar"] =
[](Calculator<math::Rational> * calc, CalcASTElem src,
bool validate_only = false) -> math::Rational {
if (src.children.empty() || (
src.children[0].token.data != L"sample" &&
src.children[0].token.data != L"population"
)) {
throw CalcASTException(src.token,
L"Type must be 'sample' or"
L" 'population'");
}
Token type = src.children[0].token;
if (src.children.size() < 2) {
throw CalcASTException(src.token,
L"Can't calculate standard variance of"
L" nothing");
} else if (type.data == L"sample" && src.children.size() < 3) {
throw CalcASTException(type,
L"Samples must have at least two"
L" elements");
}
src.children.erase(src.children.begin());
std::valarray<math::Rational> elems(math::Rational(0),
src.children.size());
for (int i = 0; i < src.children.size(); i++) {
elems[i] = calc
->execute_ast(src.children[i], validate_only);
}
if (!validate_only) {
math::Rational mean =
elems.sum() /
static_cast<int>(elems.size());
elems -= mean;
elems *= elems;
return elems.sum() / static_cast<int>(elems.size() -
(
type.data ==
L"sample"
));
}
return 0.5;
};
}
void RationalCalculator::generate_functions_help() {
std::wstring funcs_help =
L"This calculator also has a few functions to help you.\n"
L"Each function has its own help page. Functions can be"
L" used as a value wherever a value is accepted. You can"
L" use them in equations or even in other functions."
L" Here's a list of function help pages:\n\n";
for (auto & page : help_pages) {
if (page.first.substr(page.first.length() - 2) == L"()") {
funcs_help.append(page.first + L", ");
}
}
help_pages[L"functions"] = funcs_help
.substr(0, funcs_help.length() - 2);
help_pages.at(L"starthere").append(L"\n:help functions");
}
#endif //CALCULATOR_RATIONALCALCULATOR_CPP