C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_mpfr_adaptor.hpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * MPFR Adaptor type *
6 * Authors: Arash Partow and Pavel Holoborodko *
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_MPFRREAL_ADAPTOR_HPP
21#define EXPRTK_MPFRREAL_ADAPTOR_HPP
22
23
24#include <string>
25#include <mpreal.h>
26
27
28namespace exprtk
29{
30 namespace details
31 {
32 namespace numeric { namespace details
33 {
34 struct mpfrreal_type_tag;
35
36 template <typename T> inline T const_pi_impl(mpfrreal_type_tag);
37 template <typename T> inline T const_e_impl (mpfrreal_type_tag);
38 }}
39
40 inline bool is_true (const mpfr::mpreal& v);
41 inline bool is_false(const mpfr::mpreal& v);
42
43 template <typename Iterator>
44 inline bool string_to_real(Iterator& itr_external, const Iterator end, mpfr::mpreal& t, numeric::details::mpfrreal_type_tag);
45
46 }
47
48 namespace rtl { namespace io
49 {
50 namespace details
51 {
52 void print_type(const std::string&, const mpfr::mpreal& v, exprtk::details::numeric::details::mpfrreal_type_tag);
53 }
54 }}
55
56 using details::is_true;
57}
58
59#include "exprtk.hpp"
60
61namespace exprtk
62{
63 namespace details
64 {
65
66 namespace constant
67 {
68 static const mp_prec_t mpfr_precision = 128;
69 static const mp_rnd_t mpfr_round = mpfr::mpreal::get_default_rnd();
70
71 static const mpfr::mpreal e = mpfr::const_euler(mpfr_precision,mpfr_round);
72 static const mpfr::mpreal pi = mpfr::const_pi (mpfr_precision,mpfr_round);
73 static const mpfr::mpreal pi_2 = mpfr::const_pi (mpfr_precision,mpfr_round) / mpfr::mpreal( 2.0);
74 static const mpfr::mpreal pi_4 = mpfr::const_pi (mpfr_precision,mpfr_round) / mpfr::mpreal( 4.0);
75 static const mpfr::mpreal pi_180 = mpfr::const_pi (mpfr_precision,mpfr_round) / mpfr::mpreal(180.0);
76 static const mpfr::mpreal _1_pi = mpfr::mpreal( 1.0) / mpfr::const_pi(mpfr_precision,mpfr_round);
77 static const mpfr::mpreal _2_pi = mpfr::mpreal( 2.0) / mpfr::const_pi(mpfr_precision,mpfr_round);
78 static const mpfr::mpreal _180_pi = mpfr::mpreal(180.0) / mpfr::const_pi(mpfr_precision,mpfr_round);
79 static const mpfr::mpreal log2 = mpfr::const_log2 (mpfr_precision,mpfr_round);
80 static const mpfr::mpreal sqrt2 = mpfr::sqrt(mpfr::mpreal(2.0));
81 }
82
83 namespace numeric
84 {
85 namespace details
86 {
88
89 template<> struct number_type<mpfr::mpreal> { typedef mpfrreal_type_tag type; };
90
91 template <>
93 {
94 static inline mpfr::mpreal value()
95 {
96 static const mpfr::mpreal epsilon =
97 #ifndef exprtk_use_mpfr_epsilon
98 mpfr::mpreal(1.0) / mpfr::mpreal (1e+20);
99 #else
100 mpfr::machine_epsilon();
101 #endif
102 return epsilon;
103 }
104 };
105
106 inline bool is_nan_impl(const mpfr::mpreal& v, mpfrreal_type_tag)
107 {
108 return mpfr::isnan(v);
109 }
110
111 template <typename T>
112 inline int to_int32_impl(const T& v, mpfrreal_type_tag)
113 {
114 return static_cast<int>(v.toLong());
115 }
116
117 template <typename T>
118 inline long long to_int64_impl(const T& v, mpfrreal_type_tag)
119 {
120 return static_cast<long long int>(v.toLLong());
121 }
122
123 template <typename T>
124 inline long long to_uint64_impl(const T& v, mpfrreal_type_tag)
125 {
126 return static_cast<long long int>(v.toULLong());
127 }
128
129 template <typename T> inline T abs_impl(const T& v, mpfrreal_type_tag) { return mpfr::abs (v); }
130 template <typename T> inline T acos_impl(const T& v, mpfrreal_type_tag) { return mpfr::acos (v); }
131 template <typename T> inline T acosh_impl(const T& v, mpfrreal_type_tag) { return mpfr::acosh(v); }
132 template <typename T> inline T asin_impl(const T& v, mpfrreal_type_tag) { return mpfr::asin (v); }
133 template <typename T> inline T asinh_impl(const T& v, mpfrreal_type_tag) { return mpfr::asinh(v); }
134 template <typename T> inline T atan_impl(const T& v, mpfrreal_type_tag) { return mpfr::atan (v); }
135 template <typename T> inline T atanh_impl(const T& v, mpfrreal_type_tag) { return mpfr::atanh(v); }
136 template <typename T> inline T ceil_impl(const T& v, mpfrreal_type_tag) { return mpfr::ceil (v); }
137 template <typename T> inline T cos_impl(const T& v, mpfrreal_type_tag) { return mpfr::cos (v); }
138 template <typename T> inline T cosh_impl(const T& v, mpfrreal_type_tag) { return mpfr::cosh (v); }
139 template <typename T> inline T exp_impl(const T& v, mpfrreal_type_tag) { return mpfr::exp (v); }
140 template <typename T> inline T floor_impl(const T& v, mpfrreal_type_tag) { return mpfr::floor(v); }
141 template <typename T> inline T log_impl(const T& v, mpfrreal_type_tag) { return mpfr::log (v); }
142 template <typename T> inline T log10_impl(const T& v, mpfrreal_type_tag) { return mpfr::log10(v); }
143 template <typename T> inline T log2_impl(const T& v, mpfrreal_type_tag) { return mpfr::log2 (v); }
144 template <typename T> inline T neg_impl(const T& v, mpfrreal_type_tag) { return -v; }
145 template <typename T> inline T pos_impl(const T& v, mpfrreal_type_tag) { return v; }
146 template <typename T> inline T sin_impl(const T& v, mpfrreal_type_tag) { return mpfr::sin (v); }
147 template <typename T> inline T sinh_impl(const T& v, mpfrreal_type_tag) { return mpfr::sinh (v); }
148 template <typename T> inline T sqrt_impl(const T& v, mpfrreal_type_tag) { return mpfr::sqrt (v); }
149 template <typename T> inline T tan_impl(const T& v, mpfrreal_type_tag) { return mpfr::tan (v); }
150 template <typename T> inline T tanh_impl(const T& v, mpfrreal_type_tag) { return mpfr::tanh (v); }
151 template <typename T> inline T cot_impl(const T& v, mpfrreal_type_tag) { return mpfr::cot (v); }
152 template <typename T> inline T sec_impl(const T& v, mpfrreal_type_tag) { return mpfr::sec (v); }
153 template <typename T> inline T csc_impl(const T& v, mpfrreal_type_tag) { return mpfr::csc (v); }
154 template <typename T> inline T r2d_impl(const T& v, mpfrreal_type_tag) { return (v * exprtk::details::constant::_180_pi); }
155 template <typename T> inline T d2r_impl(const T& v, mpfrreal_type_tag) { return (v * exprtk::details::constant::pi_180 ); }
156 template <typename T> inline T d2g_impl(const T& v, mpfrreal_type_tag) { return (v * T(10.0) / T(9.0)); }
157 template <typename T> inline T g2d_impl(const T& v, mpfrreal_type_tag) { return (v * T(9.0) / T(10.0)); }
158 template <typename T> inline T notl_impl(const T& v, mpfrreal_type_tag) { return (v != T(0) ? T(0) : T(1)); }
159 template <typename T> inline T frac_impl(const T& v, mpfrreal_type_tag) { return mpfr::frac (v); }
160 template <typename T> inline T trunc_impl(const T& v, mpfrreal_type_tag) { return mpfr::trunc(v); }
161
162 template <typename T> inline T const_pi_impl(mpfrreal_type_tag) { return mpfr::const_pi (1024, exprtk::details::constant::mpfr_round); }
163 template <typename T> inline T const_e_impl (mpfrreal_type_tag) { return mpfr::const_euler(1024, exprtk::details::constant::mpfr_round); }
164
165 inline bool is_true_impl (const mpfr::mpreal& v)
166 {
167 return v.toBool();
168 }
169
170 inline bool is_false_impl(const mpfr::mpreal& v)
171 {
172 return !is_true_impl(v);
173 }
174
175 template <typename T>
176 inline T expm1_impl(const T& v, mpfrreal_type_tag)
177 {
178 return mpfr::expm1(v);
179 }
180
181 template <typename T>
182 inline T min_impl(const T& v0, const T& v1, mpfrreal_type_tag)
183 {
184 using std::min;
185 return min(v0,v1);
186 }
187
188 template <typename T>
189 inline T max_impl(const T& v0, const T& v1, mpfrreal_type_tag)
190 {
191 using std::max;
192 return max(v0,v1);
193 }
194
195 template <typename T>
196 inline T nequal_impl(const T& v0, const T& v1, mpfrreal_type_tag)
197 {
198 const T epsilon = epsilon_type<T>::value();
199 const T eps_norm = (mpfr::max(T(1),mpfr::max(mpfr::abs(v0),mpfr::abs(v1))) * epsilon);
200 return (mpfr::abs(v0 - v1) > eps_norm) ? T(1) : T(0);
201 }
202
203 template <typename T>
204 inline T sgn_impl(const T& v, mpfrreal_type_tag)
205 {
206 if (v > T(0)) return T(+1);
207 else if (v < T(0)) return T(-1);
208 else return T( 0);
209 }
210
211 template <typename T>
212 inline T log1p_impl(const T& v, mpfrreal_type_tag)
213 {
214 return mpfr::log1p(v);
215 }
216
217 template <typename T>
218 inline T erf_impl(const T& v, mpfrreal_type_tag)
219 {
220 return mpfr::erf(v);
221 }
222
223 template <typename T>
224 inline T erfc_impl(const T& v, mpfrreal_type_tag)
225 {
226 return mpfr::erfc(v);
227 }
228
229 template <typename T>
230 inline T ncdf_impl(const T& v, mpfrreal_type_tag)
231 {
232 static const T _05 = T(0.5);
234 }
235
236 template <typename T>
237 inline T modulus_impl(const T& v0, const T& v1, mpfrreal_type_tag)
238 {
239 return mpfr::fmod(v0,v1);
240 }
241
242 template <typename T>
243 inline T pow_impl(const T& v0, const T& v1, mpfrreal_type_tag)
244 {
245 return mpfr::pow(v0,v1);
246 }
247
248 template <typename T>
249 inline T logn_impl(const T& v0, const T& v1, mpfrreal_type_tag)
250 {
251 return mpfr::log(v0) / mpfr::log(v1);
252 }
253
254 template <typename T>
255 inline T sinc_impl(const T& v, mpfrreal_type_tag)
256 {
257 if (mpfr::abs(v) >= epsilon_type<mpfr::mpreal>::value())
258 return(mpfr::sin(v) / v);
259 else
260 return T(1);
261 }
262
263 template <typename T>
264 inline T xor_impl(const T& v0, const T& v1, mpfrreal_type_tag)
265 {
266 return (is_false_impl(v0) != is_false_impl(v1)) ? T(1) : T(0);
267 }
268
269 template <typename T>
270 inline T xnor_impl(const T& v0, const T& v1, mpfrreal_type_tag)
271 {
272 const bool v0_true = is_true_impl(v0);
273 const bool v1_true = is_true_impl(v1);
274 if ((v0_true && v1_true) || (!v0_true && !v1_true))
275 return T(1);
276 else
277 return T(0);
278 }
279
280 template <typename T>
281 inline T equal_impl(const T& v0, const T& v1, mpfrreal_type_tag)
282 {
283 const T epsilon = epsilon_type<T>::value();
284 const T eps_norm = (mpfr::max(T(1),mpfr::max(mpfr::abs(v0),mpfr::abs(v1))) * epsilon);
285 const T diff = mpfr::abs(v0 - v1);
286 return (diff <= eps_norm) ? T(1) : T(0);
287 }
288
289 template <typename T>
290 inline T round_impl(const T& v, mpfrreal_type_tag)
291 {
292 return mpfr::round(v);
293 }
294
295 template <typename T>
296 inline T roundn_impl(const T& v0, const T& v1, mpfrreal_type_tag)
297 {
298 const T p10 = mpfr::pow(T(10),mpfr::floor(v1));
299 if (v0 < T(0))
300 return T(mpfr::ceil ((v0 * p10) - T(0.5)) / p10);
301 else
302 return T(mpfr::floor((v0 * p10) + T(0.5)) / p10);
303 }
304
305 template <typename T>
306 inline bool is_integer_impl(const T& v, mpfrreal_type_tag)
307 {
308 return mpfr::isint(v);
309 }
310
311 template <typename T>
312 inline T root_impl(const T& v0, const T& v1, mpfrreal_type_tag)
313 {
314 return mpfr::pow(v0,T(1) / v1);
315 }
316
317 template <typename T>
318 inline T hypot_impl(const T& v0, const T& v1, mpfrreal_type_tag)
319 {
320 return mpfr::hypot(v0,v1);
321 }
322
323 template <typename T>
324 inline T atan2_impl(const T& v0, const T& v1, mpfrreal_type_tag)
325 {
326 return mpfr::atan2(v0,v1);
327 }
328
329 template <typename T>
330 inline T shr_impl(const T& v0, const T& v1, mpfrreal_type_tag)
331 {
332 return v0 * (T(1) / mpfr::pow(T(2.0),v1));
333 }
334
335 template <typename T>
336 inline T shl_impl(const T& v0, const T& v1, mpfrreal_type_tag)
337 {
338 return v0 * mpfr::pow(T(2.0),v1);
339 }
340
341 template <typename T>
342 inline T and_impl(const T& v0, const T& v1, mpfrreal_type_tag)
343 {
344 return (is_true_impl(v0) && is_true_impl(v1)) ? T(1) : T(0);
345 }
346
347 template <typename T>
348 inline T nand_impl(const T& v0, const T& v1, mpfrreal_type_tag)
349 {
350 return (is_false_impl(v0) || is_false_impl(v1)) ? T(1) : T(0);
351 }
352
353 template <typename T>
354 inline T or_impl(const T& v0, const T& v1, mpfrreal_type_tag)
355 {
356 return (is_true_impl(v0) || is_true_impl(v1)) ? T(1) : T(0);
357 }
358
359 template <typename T>
360 inline T nor_impl(const T& v0, const T& v1, mpfrreal_type_tag)
361 {
362 return (is_false_impl(v0) && is_false_impl(v1)) ? T(1) : T(0);
363 }
364 }
365 }
366
367 template <typename Iterator>
369 {
370 t = mpfr::mpreal(std::string(itr_external,end));
371 return true;
372 }
373
374 inline bool is_true (const mpfr::mpreal& v) { return details::numeric::details::is_true_impl (v); }
375 inline bool is_false(const mpfr::mpreal& v) { return details::numeric::details::is_false_impl(v); }
376 }
377
378 namespace rtl { namespace io
379 {
380 namespace details
381 {
382 inline void print_type(const std::string&, const mpfr::mpreal& v, exprtk::details::numeric::details::mpfrreal_type_tag)
383 {
384 #if defined(__clang__)
385 #pragma clang diagnostic push
386 #pragma clang diagnostic ignored "-Wformat-nonliteral"
387 #elif defined(__GNUC__) || defined(__GNUG__)
388 #pragma GCC diagnostic push
389 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
390 #elif defined(_MSC_VER)
391 #endif
392
393 printf("%s",v.toString().c_str());
394
395 #if defined(__clang__)
396 #pragma clang diagnostic pop
397 #elif defined(__GNUC__) || defined(__GNUG__)
398 #pragma GCC diagnostic pop
399 #elif defined(_MSC_VER)
400 #endif
401 }
402 }
403 }}
404}
405
406#endif
bool is_true(const complex_t v)
static const mp_prec_t mpfr_precision
static const mpfr::mpreal pi
static const mpfr::mpreal pi_180
static const mpfr::mpreal _2_pi
static const mpfr::mpreal e
static const mpfr::mpreal log2
static const mpfr::mpreal sqrt2
static const mpfr::mpreal pi_2
static const mpfr::mpreal _180_pi
static const mpfr::mpreal pi_4
static const mp_rnd_t mpfr_round
static const mpfr::mpreal _1_pi
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 min_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:921
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 max_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:927
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 min(const T v0, const T v1)
Definition exprtk.hpp:1492
T max(const T v0, const T v1)
Definition exprtk.hpp:1499
bool is_true(const double v)
Definition exprtk.hpp:5615
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
void print_type(const std::string &fmt, const T v, exprtk::details::numeric::details::real_type_tag)
Definition exprtk.hpp:43547