-
Notifications
You must be signed in to change notification settings - Fork 0
/
exprtkengine.hpp
193 lines (161 loc) · 5.98 KB
/
exprtkengine.hpp
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
#ifndef EXPRTKENGINE_H
#define EXPRTKENGINE_H
#include <exprtk/exprtk.hpp>
#include <QString>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QRegularExpressionMatchIterator>
#include <QException>
#include <QtTypes>
#include <cmath>
#include <numbers>
#include "dice_roll.h"
#include "exprtk_methods.hpp"
template <std::floating_point number_t>
class ExprTkEngine
{
public:
ExprTkEngine()
{
symbol_table.add_function("dice", this->m_roll);
symbol_table.add_function("rand", this->m_rand);
symbol_table.add_function("meemo", this->m_meemo);
symbol_table.add_function("GCD", this->m_GCD);
symbol_table.add_function("LCM", this->m_LCM);
symbol_table.add_function("xrt", this->m_xrt);
symbol_table.add_function("fact", this->m_fact);
symbol_table.add_function("gamma", this->m_fact);
symbol_table.add_function("atan2", this->m_atan2);
symbol_table.add_function("ln", this->m_ln);
symbol_table.add_function("log", this->m_log10);
symbol_table.add_function("logx", this->m_logx);
symbol_table.add_constant("EULER", std::numbers::e_v<number_t>);
symbol_table.add_constant("GOLDEN", std::numbers::phi_v<number_t>);
symbol_table.add_constant("E_M", std::numbers::egamma_v<number_t>);
if constexpr(std::is_same_v<number_t, long double>)
{
constexpr long double TAUl = 2.L * 3.141592653589793238L;
symbol_table.add_constant("TAU", TAUl);
symbol_table.add_constant("SILVER", 1.L + std::sqrtl(2.L));
symbol_table.add_constant("SUPERGOLD", newtonRaphson(1.5L, 1e-18L, 100));
}
else if constexpr(std::is_same_v<number_t, double>)
{
constexpr double TAU = 2. * 3.14159265358979323846;
symbol_table.add_constant("TAU", TAU);
symbol_table.add_constant("SILVER", 1. + std::sqrt(2.));
symbol_table.add_constant("SUPERGOLD", newtonRaphson(1.5, 1e-12, 100));
}
else
{
constexpr float TAUf = 2.f * 3.1415927f;
symbol_table.add_constant("TAU", TAUf);
symbol_table.add_constant("SILVER", 1.f + std::sqrtf(2.f));
symbol_table.add_constant("SUPERGOLD", newtonRaphson(1.5f, 1e-6f, 100));
}
symbol_table.add_constants();
}
number_t evaluate(const QString& expression)
{
exprtk::expression<number_t> exp;
exprtk::parser<number_t> parser;
exp.register_symbol_table(this->symbol_table);
QString e = expression;
QStringList result = e.split(' ');
for (QString& str : result)
{
QRegularExpressionMatchIterator it = re.globalMatch(str);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
str.replace(
match.captured(),
"dice(" + match.captured("number") + "," + match.captured("sides") + ")"
);
}
}
if (!parser.compile(result.join(' ').toStdString(), exp))
throw ExprTkParseException();
number_t value = exp.value();
if constexpr(std::is_same_v<number_t, long double>)
{
number_t roundedValue = std::roundl(value);
constexpr number_t roundThresh = 1e-18L;
return std::fabsl(value - roundedValue) < roundThresh ? roundedValue : value;
}
else if constexpr(std::is_same_v<number_t, double>)
{
number_t roundedValue = std::round(value);
constexpr number_t roundThresh = 1e-12;
return std::fabs(value - roundedValue) < roundThresh ? roundedValue : value;
}
else if constexpr(std::is_same_v<number_t, float>)
{
number_t roundedValue = std::roundf(value);
constexpr number_t roundThresh = 1e-6L;
return std::fabsf(value - roundedValue) < roundThresh ? roundedValue : value;
}
}
void setDiceRollTracker(DiceRollTracker* dt)
{
m_roll.tracker = dt;
}
private:
exprtk::symbol_table<number_t> symbol_table;
dice_roll<number_t> m_roll;
random_int<number_t> m_rand;
meemo_operation<number_t> m_meemo;
gcd<number_t> m_GCD;
lcm<number_t> m_LCM;
xrt<number_t> m_xrt;
factorial<number_t> m_fact;
exprtk_atan2<number_t> m_atan2;
ln<number_t> m_ln;
logb10<number_t> m_log10;
logx<number_t> m_logx;
QRegularExpression re{"\\b(?<number>\\d+)d(?<sides>\\d+)\\b"};
// Supergolden Ratio
constexpr number_t f(number_t x)
{
if constexpr(std::is_same_v<number_t, long double>)
return std::powl(x, 3.L) - std::powl(x, 2.L) - 1.L;
if constexpr(std::is_same_v<number_t, double>)
return std::pow(x, 3.) - std::pow(x, 2.) - 1.;
else
return std::powf(x, 3.f) - std::powf(x, 2.f) - 1.f;
}
constexpr number_t f_prime(number_t x)
{
if constexpr(std::is_same_v<number_t, long double>)
return 3.L * std::powl(x, 2.L) - 2.L * x;
if constexpr(std::is_same_v<number_t, double>)
return 3. * std::pow(x, 2.) - 2. * x;
else
return 3.f * std::powf(x, 2.f) - 2.f * x;
}
constexpr number_t newtonRaphson(number_t init, number_t tol, int max_iters)
{
number_t x = init;
for (int i = 0; i < max_iters; ++i)
{
number_t fx = f(x);
number_t fpx = f_prime(x);
number_t x_new = x - fx / fpx;
if (constexpr_abs(x_new - x) < tol)
{
return x_new;
}
x = x_new;
}
return x;
}
constexpr number_t constexpr_abs(number_t x)
{
if constexpr(std::is_same_v<number_t, long double>)
return x < 0.L ? -x : x;
else if constexpr(std::is_same_v<number_t, double>)
return x < 0. ? -x : x;
else
return x < 0.f ? -x : x;
}
};
#endif // EXPRTKENGINE_H