OpcUaCanOpen
CANopen OPC-UA server
exprtk_simple_example_11.cpp
Go to the documentation of this file.
1 /*
2  **************************************************************
3  * C++ Mathematical Expression Toolkit Library *
4  * *
5  * Simple Example 11 *
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 wave_program =
33  " var r := 0; "
34  " for (var i := 0; i < 1000; i += 1) "
35  " { "
36  " r += (1 / (2i + 1)) * sin((4i + 2) * pi * f * t); "
37  " }; "
38  " r *= a * (4 / pi); ";
39 
40  static const T pi = T(3.141592653589793238462643383279502);
41 
42  T f = pi / T(10);
43  T t = T(0);
44  T a = T(10);
45 
46  symbol_table_t symbol_table;
47  symbol_table.add_variable("f",f);
48  symbol_table.add_variable("t",t);
49  symbol_table.add_variable("a",a);
50  symbol_table.add_constants();
51 
52  expression_t expression;
53  expression.register_symbol_table(symbol_table);
54 
56  parser.compile(wave_program,expression);
57 
58  const T delta = (T(4) * pi) / T(1000);
59 
60  for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta)
61  {
62  T result = expression.value();
63  printf("%19.15f\t%19.15f\n",t,result);
64  }
65 }
66 
67 int main()
68 {
69  square_wave2<double>();
70  return 0;
71 }