OpcUaCanOpen
CANopen OPC-UA server
exprtk_simple_example_08.cpp
Go to the documentation of this file.
1 /*
2  **************************************************************
3  * C++ Mathematical Expression Toolkit Library *
4  * *
5  * Simple Example 8 *
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 void composite()
27 {
31  typedef exprtk::parser_error::type err_t;
32  typedef exprtk::function_compositor<T> compositor_t;
33  typedef typename compositor_t::function function_t;
34 
35  compositor_t compositor;
36 
37  T x = T(1);
38  T y = T(2);
39 
40  symbol_table_t& symbol_table = compositor.symbol_table();
41  symbol_table.add_constants();
42  symbol_table.add_variable("x",x);
43  symbol_table.add_variable("y",y);
44 
45  compositor
46  .add(
47  function_t("f","sin(x / pi)","x")); // f(x) = sin(x / pi)
48 
49  compositor
50  .add(
51  function_t("g","3*[f(x) + f(y)]","x","y")); // g(x,y) = 3[f(x) + f(y)]
52 
53  std::string expression_string = "g(1 + f(x), f(y) / 2)";
54 
55  expression_t expression;
56  expression.register_symbol_table(symbol_table);
57 
59 
60  if (!parser.compile(expression_string,expression))
61  {
62  printf("Error: %s\tExpression: %s\n",
63  parser.error().c_str(),
64  expression_string.c_str());
65 
66  for (std::size_t i = 0; i < parser.error_count(); ++i)
67  {
68  err_t error = parser.get_error(i);
69 
70  printf("Error: %02d Position: %02d Type: [%14s] Msg: %s\tExpression: %s\n",
71  static_cast<unsigned int>(i),
72  static_cast<unsigned int>(error.token.position),
73  exprtk::parser_error::to_str(error.mode).c_str(),
74  error.diagnostic.c_str(),
75  expression_string.c_str());
76  }
77 
78  return;
79  }
80 
81  T result = expression.value();
82 
83  printf("%s = %e\n", expression_string.c_str(), result);
84 }
85 
86 int main()
87 {
88  composite<double>();
89  return 0;
90 }