C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_real_adaptor.hpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Custom Real type Adaptor *
6 * Authors: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#ifndef EXPRTK_REAL_ADAPTOR_HPP
21#define EXPRTK_REAL_ADAPTOR_HPP
22
23
24#include <string>
25#include "real_type.hpp"
26
27
28namespace exprtk
29{
30 namespace details
31 {
32 namespace numeric { namespace details
33 {
34 struct my_real_type_tag;
35
36 template <typename T> inline T const_pi_impl(my_real_type_tag);
37 template <typename T> inline T const_e_impl (my_real_type_tag);
38 }}
39
40 inline bool is_true (const real::type v);
41 inline bool is_false(const real::type v);
42
43 template <typename Iterator>
44 inline bool string_to_real(Iterator& itr_external, const Iterator end, real::type& t, details::numeric::details::my_real_type_tag);
45 }
46
47 namespace rtl { namespace io
48 {
49 namespace details
50 {
52 }
53 }}
54
55 using details::is_true;
56}
57
58#include "exprtk.hpp"
59
60namespace exprtk
61{
62 namespace details
63 {
64 namespace numeric
65 {
66 namespace details
67 {
69
70 template<> struct number_type<real::type> { typedef my_real_type_tag type; };
71
72 template <>
73 struct epsilon_type<real::type>
74 {
75 static inline real::type value()
76 {
77 const real::type epsilon = real::type(0.000000000100);
78
79 return epsilon;
80 }
81 };
82
84 {
85 return v.d_ != v.d_;
86 }
87
88 template <typename T> inline T abs_impl(const T v, my_real_type_tag) { return real::abs (v); }
89 template <typename T> inline T acos_impl(const T v, my_real_type_tag) { return real::acos (v); }
90 template <typename T> inline T acosh_impl(const T v, my_real_type_tag) { return real::acosh(v); }
91 template <typename T> inline T asin_impl(const T v, my_real_type_tag) { return real::asin (v); }
92 template <typename T> inline T asinh_impl(const T v, my_real_type_tag) { return real::asinh(v); }
93 template <typename T> inline T atan_impl(const T v, my_real_type_tag) { return real::atan (v); }
94 template <typename T> inline T atanh_impl(const T v, my_real_type_tag) { return real::atanh(v); }
95 template <typename T> inline T ceil_impl(const T v, my_real_type_tag) { return real::ceil (v); }
96 template <typename T> inline T cos_impl(const T v, my_real_type_tag) { return real::cos (v); }
97 template <typename T> inline T cosh_impl(const T v, my_real_type_tag) { return real::cosh (v); }
98 template <typename T> inline T exp_impl(const T v, my_real_type_tag) { return real::exp (v); }
99 template <typename T> inline T floor_impl(const T v, my_real_type_tag) { return real::floor(v); }
100 template <typename T> inline T log_impl(const T v, my_real_type_tag) { return real::log (v); }
101 template <typename T> inline T log10_impl(const T v, my_real_type_tag) { return real::log10(v); }
102 template <typename T> inline T log2_impl(const T v, my_real_type_tag) { return real::log2 (v); }
103 template <typename T> inline T neg_impl(const T v, my_real_type_tag) { return -v; }
104 template <typename T> inline T pos_impl(const T v, my_real_type_tag) { return v; }
105 template <typename T> inline T sin_impl(const T v, my_real_type_tag) { return real::sin (v); }
106 template <typename T> inline T sinh_impl(const T v, my_real_type_tag) { return real::sinh (v); }
107 template <typename T> inline T sqrt_impl(const T v, my_real_type_tag) { return real::sqrt (v); }
108 template <typename T> inline T tan_impl(const T v, my_real_type_tag) { return real::tan (v); }
109 template <typename T> inline T tanh_impl(const T v, my_real_type_tag) { return real::tanh (v); }
110 template <typename T> inline T cot_impl(const T v, my_real_type_tag) { return real::cot (v); }
111 template <typename T> inline T sec_impl(const T v, my_real_type_tag) { return real::sec (v); }
112 template <typename T> inline T csc_impl(const T v, my_real_type_tag) { return real::csc (v); }
113 template <typename T> inline T r2d_impl(const T v, my_real_type_tag) { return real::r2d (v); }
114 template <typename T> inline T d2r_impl(const T v, my_real_type_tag) { return real::d2r (v); }
115 template <typename T> inline T d2g_impl(const T v, my_real_type_tag) { return real::d2g (v); }
116 template <typename T> inline T g2d_impl(const T v, my_real_type_tag) { return real::g2d (v); }
117 template <typename T> inline T notl_impl(const T v, my_real_type_tag) { return real::notl (v); }
118 template <typename T> inline T frac_impl(const T v, my_real_type_tag) { return real::frac (v); }
119 template <typename T> inline T trunc_impl(const T v, my_real_type_tag) { return real::trunc(v); }
120
121 template <typename T> inline T const_pi_impl(my_real_type_tag) { return real::details::constant::pi; }
122 template <typename T> inline T const_e_impl (my_real_type_tag) { return real::details::constant::e; }
123
124 template <typename T>
125 inline int to_int32_impl(const T v, my_real_type_tag)
126 {
127 return static_cast<int>(v);
128 }
129
130 template <typename T>
131 inline long long to_int64_impl(const T v, my_real_type_tag)
132 {
133 return static_cast<long long int>(v);
134 }
135
136 template <typename T>
137 inline unsigned long long to_uint64_impl(const T v, my_real_type_tag)
138 {
139 return static_cast<unsigned long long int>(v);
140 }
141
142 inline bool is_true_impl (const real::type v)
143 {
144 return (0.0 != v);
145 }
146
147 inline bool is_false_impl(const real::type v)
148 {
149 return (0.0 == v);
150 }
151
152 template <typename T>
154 {
155 if (abs(v) < T(0.00001))
156 return v + (T(0.5) * v * v);
157 else
158 return exp(v) - T(1);
159 }
160
161 template <typename T>
162 inline T nequal_impl(const T v0, const T v1, my_real_type_tag)
163 {
164 const T epsilon = epsilon_type<T>::value();
165 const T eps_norm = (std::max(T(1),std::max(abs_impl(v0,my_real_type_tag()),abs_impl(v1,my_real_type_tag()))) * epsilon);
166 return (abs_impl(v0 - v1,my_real_type_tag()) > eps_norm) ? T(1) : T(0);
167 }
168
169 template <typename T>
170 inline T sgn_impl(const T v, my_real_type_tag)
171 {
172 if (v > T(0)) return T(+1);
173 else if (v < T(0)) return T(-1);
174 else return T( 0);
175 }
176
177 template <typename T>
179 {
180 if (v > T(-1))
181 {
182 if (abs_impl(v,my_real_type_tag()) > T(0.0001))
183 {
184 return log_impl(T(1) + v,my_real_type_tag());
185 }
186 else
187 return (T(-0.5) * v + T(1)) * v;
188 }
189 else
190 return T(std::numeric_limits<T>::quiet_NaN());
191 }
192
193 template <typename T>
195 {
196 return real::erf(v);
197 }
198
199 template <typename T>
201 {
202 return T(1) - erf_impl(v,my_real_type_tag());
203 }
204
205 template <typename T>
207 {
208 return T(0.5) * erfc_impl(-(v / T(numeric::constant::sqrt2)),my_real_type_tag());
209 }
210
211 template <typename T>
212 inline T modulus_impl(const T v0, const T v1, my_real_type_tag)
213 {
214 return modulus(v0,v1);
215 }
216
217 template <typename T>
218 inline T pow_impl(const T v0, const T v1, my_real_type_tag)
219 {
220 return real::pow(v0,v1);
221 }
222
223 template <typename T>
224 inline T logn_impl(const T v0, const T v1, my_real_type_tag)
225 {
226 return log(v0) / log(v1);
227 }
228
229 template <typename T>
231 {
232 if (abs_impl(v,my_real_type_tag()) >= std::numeric_limits<T>::epsilon())
233 return(sin_impl(v,my_real_type_tag()) / v);
234 else
235 return T(1);
236 }
237
238 template <typename T>
239 inline T xor_impl(const T v0, const T v1, my_real_type_tag)
240 {
241 return (is_false_impl(v0) != is_false_impl(v1)) ? T(1) : T(0);
242 }
243
244 template <typename T>
245 inline T xnor_impl(const T v0, const T v1, my_real_type_tag)
246 {
247 const bool v0_true = is_true_impl(v0);
248 const bool v1_true = is_true_impl(v1);
249 if ((v0_true && v1_true) || (!v0_true && !v1_true))
250 return T(1);
251 else
252 return T(0);
253 }
254
255 template <typename T>
256 inline T equal_impl(const T v0, const T v1, my_real_type_tag)
257 {
258 const T epsilon = epsilon_type<T>::value();
259 const T eps_norm = (max(T(1),max(abs_impl(v0,my_real_type_tag()),abs_impl(v1,my_real_type_tag()))) * epsilon);
260 return (abs_impl(v0 - v1,my_real_type_tag()) <= eps_norm) ? T(1) : T(0);
261 }
262
263 template <typename T>
265 {
266 return ((v < T(0)) ? ceil(v - T(0.5)) : floor(v + T(0.5)));
267 }
268
269 template <typename T>
270 inline T roundn_impl(const T v0, const T v1, my_real_type_tag)
271 {
272 const int index = std::max<int>(0, std::min<int>(pow10_size - 1, static_cast<int>(floor(v1))));
273 const T p10 = T(pow10[index]);
274 if (v0 < T(0))
275 return T(ceil ((v0 * p10) - T(0.5)) / p10);
276 else
277 return T(floor((v0 * p10) + T(0.5)) / p10);
278 }
279
280 template <typename T>
281 inline bool is_integer_impl(const T v, my_real_type_tag)
282 {
283 return (T(0) == modulus_impl(v,T(1),my_real_type_tag()));
284 }
285
286 template <typename T>
287 inline T root_impl(const T v0, const T v1, my_real_type_tag)
288 {
289 if (v0 < T(0))
290 {
291 return (v1 == trunc_impl(v1, my_real_type_tag())) &&
292 (modulus_impl(v1, T(2), my_real_type_tag()) != T(0)) ?
293 -pow(abs_impl(v0, my_real_type_tag()), T(1) / v1) :
294 std::numeric_limits<T>::quiet_NaN();
295 }
296
297 return pow(v0, T(1) / v1);
298 }
299
300 template <typename T>
301 inline T hypot_impl(const T v0, const T v1, my_real_type_tag)
302 {
303 return sqrt((v0 * v0) + (v1 * v1));
304 }
305
306 template <typename T>
307 inline T atan2_impl(const T v0, const T v1, my_real_type_tag)
308 {
309 return std::atan2(v0.d_,v1.d_);
310 }
311
312 template <typename T>
313 inline T shr_impl(const T v0, const T v1, my_real_type_tag)
314 {
315 return v0 * (T(1) / pow(T(2),v1));
316 }
317
318 template <typename T>
319 inline T shl_impl(const T v0, const T v1, my_real_type_tag)
320 {
321 return v0 * pow(T(2),v1);
322 }
323
324 template <typename T>
325 inline T and_impl(const T v0, const T v1, my_real_type_tag)
326 {
327 return (is_true_impl(v0) && is_true_impl(v1)) ? T(1) : T(0);
328 }
329
330 template <typename T>
331 inline T nand_impl(const T v0, const T v1, my_real_type_tag)
332 {
333 return (is_false_impl(v0) || is_false_impl(v1)) ? T(1) : T(0);
334 }
335
336 template <typename T>
337 inline T or_impl(const T v0, const T v1, my_real_type_tag)
338 {
339 return (is_true_impl(v0) || is_true_impl(v1)) ? T(1) : T(0);
340 }
341
342 template <typename T>
343 inline T nor_impl(const T v0, const T v1, my_real_type_tag)
344 {
345 return (is_false_impl(v0) && is_false_impl(v1)) ? T(1) : T(0);
346 }
347 }
348 }
349
350 template <typename Iterator>
356
357 inline bool is_true (const real::type v) { return real::is_true(v); }
358 inline bool is_false(const real::type v) { return real::is_false(v); }
359 }
360
361 namespace rtl { namespace io
362 {
363 namespace details
364 {
365 inline void print_type(const std::string& fmt, const real::type& v, exprtk::details::numeric::details::my_real_type_tag)
366 {
367 #if defined(__clang__)
368 #pragma clang diagnostic push
369 #pragma clang diagnostic ignored "-Wformat-nonliteral"
370 #elif defined(__GNUC__) || defined(__GNUG__)
371 #pragma GCC diagnostic push
372 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
373 #elif defined(_MSC_VER)
374 #endif
375
376 printf(fmt.c_str(),static_cast<double>(v));
377
378 #if defined(__clang__)
379 #pragma clang diagnostic pop
380 #elif defined(__GNUC__) || defined(__GNUG__)
381 #pragma GCC diagnostic pop
382 #elif defined(_MSC_VER)
383 #endif
384 }
385 }
386 }}
387}
388
389#endif
bool is_true(const complex_t v)
float erf_impl(const float v)
Definition exprtk.hpp:1211
bool is_nan_impl(const T v, real_type_tag)
Definition exprtk.hpp:886
T floor_impl(const T v, real_type_tag)
Definition exprtk.hpp:1428
T ceil_impl(const T v, real_type_tag)
Definition exprtk.hpp:1424
T shr_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1084
T tanh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1438
T acosh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1355
T nor_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1160
bool is_integer_impl(const T &v, real_type_tag)
Definition exprtk.hpp:1484
T sinc_impl(const T v, real_type_tag)
Definition exprtk.hpp:1291
T asin_impl(const T v, real_type_tag)
Definition exprtk.hpp:1422
T log10_impl(const T v, real_type_tag)
Definition exprtk.hpp:1430
T r2d_impl(const T v, real_type_tag)
Definition exprtk.hpp:1442
T shl_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1096
T logn_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1004
T sqrt_impl(const T v, real_type_tag)
Definition exprtk.hpp:1436
T xnor_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1184
T asinh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1361
float erfc_impl(const float v)
Definition exprtk.hpp:1256
T d2r_impl(const T v, real_type_tag)
Definition exprtk.hpp:1443
T neg_impl(const T v, real_type_tag)
Definition exprtk.hpp:1432
T root_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1016
T and_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1124
T pos_impl(const T v, real_type_tag)
Definition exprtk.hpp:1433
T log_impl(const T v, real_type_tag)
Definition exprtk.hpp:1429
_uint64_t to_uint64_impl(const T v, real_type_tag)
Definition exprtk.hpp:904
T ncdf_impl(const T v, real_type_tag)
Definition exprtk.hpp:1279
T csc_impl(const T v, real_type_tag)
Definition exprtk.hpp:1441
_int64_t to_int64_impl(const T v, real_type_tag)
Definition exprtk.hpp:898
T log2_impl(const T v, real_type_tag)
Definition exprtk.hpp:1431
T cot_impl(const T v, real_type_tag)
Definition exprtk.hpp:1439
T or_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1148
T cos_impl(const T v, real_type_tag)
Definition exprtk.hpp:1425
T sinh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1435
T roundn_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1042
T sec_impl(const T v, real_type_tag)
Definition exprtk.hpp:1440
T atan2_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1072
T pow_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:992
T modulus_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:980
T log1p_impl(const T v, real_type_tag)
Definition exprtk.hpp:1394
T trunc_impl(const T v, real_type_tag)
Definition exprtk.hpp:1373
T frac_impl(const T v, real_type_tag)
Definition exprtk.hpp:1447
T hypot_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1060
T nand_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1136
T equal_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:940
T nequal_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:959
T g2d_impl(const T v, real_type_tag)
Definition exprtk.hpp:1445
T exp_impl(const T v, real_type_tag)
Definition exprtk.hpp:1427
T sin_impl(const T v, real_type_tag)
Definition exprtk.hpp:1434
T round_impl(const T v, real_type_tag)
Definition exprtk.hpp:1036
T abs_impl(const T v, real_type_tag)
Definition exprtk.hpp:922
T sgn_impl(const T v, real_type_tag)
Definition exprtk.hpp:1108
T d2g_impl(const T v, real_type_tag)
Definition exprtk.hpp:1444
T acos_impl(const T v, real_type_tag)
Definition exprtk.hpp:1421
T tan_impl(const T v, real_type_tag)
Definition exprtk.hpp:1437
T xor_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1172
T expm1_impl(const T v, real_type_tag)
Definition exprtk.hpp:1379
int to_int32_impl(const T v, real_type_tag)
Definition exprtk.hpp:892
T atanh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1367
T atan_impl(const T v, real_type_tag)
Definition exprtk.hpp:1423
T notl_impl(const T v, real_type_tag)
Definition exprtk.hpp:1446
T cosh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1426
T pow(const T v0, const T v1)
Definition exprtk.hpp:1568
T modulus(const T v0, const T v1)
Definition exprtk.hpp:1561
T max(const T v0, const T v1)
Definition exprtk.hpp:1540
bool is_true(const double v)
Definition exprtk.hpp:5742
static const double pow10[]
Definition exprtk.hpp:804
bool string_to_real(Iterator &itr_external, const Iterator end, T &t, numeric::details::real_type_tag)
Definition exprtk.hpp:1973
bool is_false(const expression_node< T > *node)
Definition exprtk.hpp:5770
bool match_impl(const Iterator pattern_begin, const Iterator pattern_end, const Iterator data_begin, const Iterator data_end, const typename std::iterator_traits< Iterator >::value_type &zero_or_more, const typename std::iterator_traits< Iterator >::value_type &exactly_one)
Definition exprtk.hpp:605
static const std::size_t pow10_size
Definition exprtk.hpp:813
void print_type(const std::string &fmt, const T v, exprtk::details::numeric::details::real_type_tag)
Definition exprtk.hpp:44040
static const double pi
static const double e
type sin(const type v)
type cot(const type v)
type r2d(const type v)
type pow(const type v0, const type v1)
type acos(const type v)
type d2r(const type v)
type notl(const type v)
type floor(const type v)
type cosh(const type v)
type erf(type v)
type log10(const type v)
bool is_false(const type v)
type acosh(const type v)
type csc(const type v)
type cos(const type v)
type sinh(const type v)
type exp(const type v)
type frac(const type v)
type trunc(const type v)
type log2(const type v)
type asinh(const type v)
type tan(const type v)
type asin(const type v)
type g2d(const type v)
type ceil(const type v)
type atan(const type v)
type d2g(const type v)
type abs(const type v)
type log(const type v)
type atanh(const type v)
type sqrt(const type v)
type sec(const type v)
bool is_true(const type v)
type tanh(const type v)
double d_
Definition real_type.hpp:82