OpcUaCanOpen
CANopen OPC-UA server
exprtk_simple_example_02.cpp
Go to the documentation of this file.
1 /*
2  **************************************************************
3  * C++ Mathematical Expression Toolkit Library *
4  * *
5  * Simple Example 2 *
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>
27 {
31 
32  std::string expr_string = "a*(4/pi)*"
33  "((1 /1)*sin( 2*pi*f*t)+(1 /3)*sin( 6*pi*f*t)+"
34  " (1 /5)*sin(10*pi*f*t)+(1 /7)*sin(14*pi*f*t)+"
35  " (1 /9)*sin(18*pi*f*t)+(1/11)*sin(22*pi*f*t)+"
36  " (1/13)*sin(26*pi*f*t)+(1/15)*sin(30*pi*f*t)+"
37  " (1/17)*sin(34*pi*f*t)+(1/19)*sin(38*pi*f*t)+"
38  " (1/21)*sin(42*pi*f*t)+(1/23)*sin(46*pi*f*t)+"
39  " (1/25)*sin(50*pi*f*t)+(1/27)*sin(54*pi*f*t))";
40 
41  static const T pi = T(3.141592653589793238462643383279502);
42 
43  const T f = pi / T(10);
44  const T a = T(10);
45  T t = T(0);
46 
47  symbol_table_t symbol_table;
48  symbol_table.add_variable("t",t);
49  symbol_table.add_constant("f",f);
50  symbol_table.add_constant("a",a);
51  symbol_table.add_constants();
52 
53  expression_t expression;
54  expression.register_symbol_table(symbol_table);
55 
57  parser.compile(expr_string,expression);
58 
59  const T delta = (T(4) * pi) / T(1000);
60 
61  for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta)
62  {
63  T result = expression.value();
64  printf("%19.15f\t%19.15f\n",t,result);
65  }
66 }
67 
68 int main()
69 {
70  square_wave<double>();
71  return 0;
72 }