OpcUaCanOpen
CANopen OPC-UA server
exprtk_simple_example_05.cpp
Go to the documentation of this file.
1 /*
2  **************************************************************
3  * C++ Mathematical Expression Toolkit Library *
4  * *
5  * Simple Example 5 *
6  * Author: Arash Partow (1999-2019) *
7  * URL: http://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  * http://www.opensource.org/licenses/MIT *
14  * *
15  **************************************************************
16 */
17 
18 
19 #include <cstdio>
20 #include <string>
21 
22 #include "exprtk.hpp"
23 
24 
25 template <typename T>
26 struct myfunc : public exprtk::ifunction<T>
27 {
29 
31  : exprtk::ifunction<T>(2)
33 
34  inline T operator()(const T& v1, const T& v2)
35  {
36  return T(1) + (v1 * v2) / T(3);
37  }
38 };
39 
40 template <typename T>
41 inline T myotherfunc(T v0, T v1, T v2)
42 {
43  return std::abs(v0 - v1) * v2;
44 }
45 
46 template <typename T>
48 {
52 
53  std::string expression_string =
54  "myfunc(sin(x / pi), otherfunc(3 * y, x / 2, x * y))";
55 
56  T x = T(1);
57  T y = T(2);
58  myfunc<T> mf;
59 
60  symbol_table_t symbol_table;
61  symbol_table.add_variable("x",x);
62  symbol_table.add_variable("y",y);
63  symbol_table.add_function("myfunc",mf);
64  symbol_table.add_function("otherfunc",myotherfunc);
65  symbol_table.add_constants();
66 
67  expression_t expression;
68  expression.register_symbol_table(symbol_table);
69 
71  parser.compile(expression_string,expression);
72 
73  T result = expression.value();
74  printf("Result: %10.5f\n",result);
75 }
76 
77 int main()
78 {
79  custom_function<double>();
80  return 0;
81 }