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 return pow(v0,T(1) / v1);
290 }
291
292 template <typename T>
293 inline T hypot_impl(const T v0, const T v1, my_real_type_tag)
294 {
295 return sqrt((v0 * v0) + (v1 * v1));
296 }
297
298 template <typename T>
299 inline T atan2_impl(const T v0, const T v1, my_real_type_tag)
300 {
301 return std::atan2(v0.d_,v1.d_);
302 }
303
304 template <typename T>
305 inline T shr_impl(const T v0, const T v1, my_real_type_tag)
306 {
307 return v0 * (T(1) / pow(T(2),v1));
308 }
309
310 template <typename T>
311 inline T shl_impl(const T v0, const T v1, my_real_type_tag)
312 {
313 return v0 * pow(T(2),v1);
314 }
315
316 template <typename T>
317 inline T and_impl(const T v0, const T v1, my_real_type_tag)
318 {
319 return (is_true_impl(v0) && is_true_impl(v1)) ? T(1) : T(0);
320 }
321
322 template <typename T>
323 inline T nand_impl(const T v0, const T v1, my_real_type_tag)
324 {
325 return (is_false_impl(v0) || is_false_impl(v1)) ? T(1) : T(0);
326 }
327
328 template <typename T>
329 inline T or_impl(const T v0, const T v1, my_real_type_tag)
330 {
331 return (is_true_impl(v0) || is_true_impl(v1)) ? T(1) : T(0);
332 }
333
334 template <typename T>
335 inline T nor_impl(const T v0, const T v1, my_real_type_tag)
336 {
337 return (is_false_impl(v0) && is_false_impl(v1)) ? T(1) : T(0);
338 }
339 }
340 }
341
342 template <typename Iterator>
348
349 inline bool is_true (const real::type v) { return real::is_true(v); }
350 inline bool is_false(const real::type v) { return real::is_false(v); }
351 }
352
353 namespace rtl { namespace io
354 {
355 namespace details
356 {
357 inline void print_type(const std::string& fmt, const real::type& v, exprtk::details::numeric::details::my_real_type_tag)
358 {
359 #if defined(__clang__)
360 #pragma clang diagnostic push
361 #pragma clang diagnostic ignored "-Wformat-nonliteral"
362 #elif defined(__GNUC__) || defined(__GNUG__)
363 #pragma GCC diagnostic push
364 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
365 #elif defined(_MSC_VER)
366 #endif
367
368 printf(fmt.c_str(),static_cast<double>(v));
369
370 #if defined(__clang__)
371 #pragma clang diagnostic pop
372 #elif defined(__GNUC__) || defined(__GNUG__)
373 #pragma GCC diagnostic pop
374 #elif defined(_MSC_VER)
375 #endif
376 }
377 }
378 }}
379}
380
381#endif
bool is_true(const complex_t v)
float erf_impl(const float v)
Definition exprtk.hpp:1247
bool is_nan_impl(const T v, real_type_tag)
Definition exprtk.hpp:879
T floor_impl(const T v, real_type_tag)
Definition exprtk.hpp:1386
T ceil_impl(const T v, real_type_tag)
Definition exprtk.hpp:1382
T shr_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1120
T tanh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1396
T acosh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1361
T nor_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1196
bool is_integer_impl(const T &v, real_type_tag)
Definition exprtk.hpp:1443
T sinc_impl(const T v, real_type_tag)
Definition exprtk.hpp:1327
T asin_impl(const T v, real_type_tag)
Definition exprtk.hpp:1380
T log10_impl(const T v, real_type_tag)
Definition exprtk.hpp:1388
T r2d_impl(const T v, real_type_tag)
Definition exprtk.hpp:1400
T shl_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1132
T logn_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1013
T sqrt_impl(const T v, real_type_tag)
Definition exprtk.hpp:1394
T xnor_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1220
T asinh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1367
float erfc_impl(const float v)
Definition exprtk.hpp:1292
T d2r_impl(const T v, real_type_tag)
Definition exprtk.hpp:1401
T neg_impl(const T v, real_type_tag)
Definition exprtk.hpp:1390
T root_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1052
T and_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1160
T pos_impl(const T v, real_type_tag)
Definition exprtk.hpp:1391
T log_impl(const T v, real_type_tag)
Definition exprtk.hpp:1387
_uint64_t to_uint64_impl(const T v, real_type_tag)
Definition exprtk.hpp:897
T ncdf_impl(const T v, real_type_tag)
Definition exprtk.hpp:1315
T csc_impl(const T v, real_type_tag)
Definition exprtk.hpp:1399
_int64_t to_int64_impl(const T v, real_type_tag)
Definition exprtk.hpp:891
T log2_impl(const T v, real_type_tag)
Definition exprtk.hpp:1389
T cot_impl(const T v, real_type_tag)
Definition exprtk.hpp:1397
T or_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1184
T cos_impl(const T v, real_type_tag)
Definition exprtk.hpp:1383
T sinh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1393
T roundn_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1078
T sec_impl(const T v, real_type_tag)
Definition exprtk.hpp:1398
T atan2_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1108
T pow_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1001
T modulus_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:989
T log1p_impl(const T v, real_type_tag)
Definition exprtk.hpp:1025
T trunc_impl(const T v, real_type_tag)
Definition exprtk.hpp:1406
T frac_impl(const T v, real_type_tag)
Definition exprtk.hpp:1405
T hypot_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1096
T nand_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1172
T equal_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:933
T nequal_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:968
T g2d_impl(const T v, real_type_tag)
Definition exprtk.hpp:1403
T exp_impl(const T v, real_type_tag)
Definition exprtk.hpp:1385
T sin_impl(const T v, real_type_tag)
Definition exprtk.hpp:1392
T round_impl(const T v, real_type_tag)
Definition exprtk.hpp:1072
T abs_impl(const T v, real_type_tag)
Definition exprtk.hpp:915
T sgn_impl(const T v, real_type_tag)
Definition exprtk.hpp:1144
T d2g_impl(const T v, real_type_tag)
Definition exprtk.hpp:1402
T acos_impl(const T v, real_type_tag)
Definition exprtk.hpp:1379
T tan_impl(const T v, real_type_tag)
Definition exprtk.hpp:1395
T xor_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:1208
T expm1_impl(const T v, real_type_tag)
Definition exprtk.hpp:952
int to_int32_impl(const T v, real_type_tag)
Definition exprtk.hpp:885
T atanh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1373
T atan_impl(const T v, real_type_tag)
Definition exprtk.hpp:1381
T notl_impl(const T v, real_type_tag)
Definition exprtk.hpp:1404
T cosh_impl(const T v, real_type_tag)
Definition exprtk.hpp:1384
T pow(const T v0, const T v1)
Definition exprtk.hpp:1527
T modulus(const T v0, const T v1)
Definition exprtk.hpp:1520
T max(const T v0, const T v1)
Definition exprtk.hpp:1499
bool is_true(const double v)
Definition exprtk.hpp:5615
static const double pow10[]
Definition exprtk.hpp:797
bool string_to_real(Iterator &itr_external, const Iterator end, T &t, numeric::details::real_type_tag)
Definition exprtk.hpp:1932
bool is_false(const expression_node< T > *node)
Definition exprtk.hpp:5643
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:598
static const std::size_t pow10_size
Definition exprtk.hpp:806
void print_type(const std::string &fmt, const T v, exprtk::details::numeric::details::real_type_tag)
Definition exprtk.hpp:43547
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