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 if (v0 < T(0))
315 {
316 return (v1 == trunc_impl(v1, mpfrreal_type_tag())) &&
317 (modulus_impl(v1, T(2), mpfrreal_type_tag()) != T(0)) ?
318 -pow(abs_impl(v0, mpfrreal_type_tag()), T(1) / v1) :
319 T().setNan();
320 }
321
322 return pow(v0, T(1) / v1);
323 }
324
325 template <typename T>
326 inline T hypot_impl(const T& v0, const T& v1, mpfrreal_type_tag)
327 {
328 return mpfr::hypot(v0,v1);
329 }
330
331 template <typename T>
332 inline T atan2_impl(const T& v0, const T& v1, mpfrreal_type_tag)
333 {
334 return mpfr::atan2(v0,v1);
335 }
336
337 template <typename T>
338 inline T shr_impl(const T& v0, const T& v1, mpfrreal_type_tag)
339 {
340 return v0 * (T(1) / mpfr::pow(T(2.0),v1));
341 }
342
343 template <typename T>
344 inline T shl_impl(const T& v0, const T& v1, mpfrreal_type_tag)
345 {
346 return v0 * mpfr::pow(T(2.0),v1);
347 }
348
349 template <typename T>
350 inline T and_impl(const T& v0, const T& v1, mpfrreal_type_tag)
351 {
352 return (is_true_impl(v0) && is_true_impl(v1)) ? T(1) : T(0);
353 }
354
355 template <typename T>
356 inline T nand_impl(const T& v0, const T& v1, mpfrreal_type_tag)
357 {
358 return (is_false_impl(v0) || is_false_impl(v1)) ? T(1) : T(0);
359 }
360
361 template <typename T>
362 inline T or_impl(const T& v0, const T& v1, mpfrreal_type_tag)
363 {
364 return (is_true_impl(v0) || is_true_impl(v1)) ? T(1) : T(0);
365 }
366
367 template <typename T>
368 inline T nor_impl(const T& v0, const T& v1, mpfrreal_type_tag)
369 {
370 return (is_false_impl(v0) && is_false_impl(v1)) ? T(1) : T(0);
371 }
372 }
373 }
374
375 template <typename Iterator>
377 {
378 t = mpfr::mpreal(std::string(itr_external,end));
379 return true;
380 }
381
382 inline bool is_true (const mpfr::mpreal& v) { return details::numeric::details::is_true_impl (v); }
383 inline bool is_false(const mpfr::mpreal& v) { return details::numeric::details::is_false_impl(v); }
384 }
385
386 namespace rtl { namespace io
387 {
388 namespace details
389 {
390 inline void print_type(const std::string&, const mpfr::mpreal& v, exprtk::details::numeric::details::mpfrreal_type_tag)
391 {
392 #if defined(__clang__)
393 #pragma clang diagnostic push
394 #pragma clang diagnostic ignored "-Wformat-nonliteral"
395 #elif defined(__GNUC__) || defined(__GNUG__)
396 #pragma GCC diagnostic push
397 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
398 #elif defined(_MSC_VER)
399 #endif
400
401 printf("%s",v.toString().c_str());
402
403 #if defined(__clang__)
404 #pragma clang diagnostic pop
405 #elif defined(__GNUC__) || defined(__GNUG__)
406 #pragma GCC diagnostic pop
407 #elif defined(_MSC_VER)
408 #endif
409 }
410 }
411 }}
412}
413
414#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: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 min_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:928
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 max_impl(const T v0, const T v1, real_type_tag)
Definition exprtk.hpp:934
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 min(const T v0, const T v1)
Definition exprtk.hpp:1533
T max(const T v0, const T v1)
Definition exprtk.hpp:1540
bool is_true(const double v)
Definition exprtk.hpp:5742
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
void print_type(const std::string &fmt, const T v, exprtk::details::numeric::details::real_type_tag)
Definition exprtk.hpp:44040