exprtk_simple_example_01.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. **************************************************************
  3. * C++ Mathematical Expression Toolkit Library *
  4. * *
  5. * Simple Example 1 *
  6. * Author: Arash Partow (1999-2020) *
  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. #include <cstdio>
  18. #include <string>
  19. #include "exprtk.hpp"
  20. template <typename T>
  21. void trig_function()
  22. {
  23. typedef exprtk::symbol_table<T> symbol_table_t;
  24. typedef exprtk::expression<T> expression_t;
  25. typedef exprtk::parser<T> parser_t;
  26. const std::string expression_string =
  27. "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";
  28. T x;
  29. symbol_table_t symbol_table;
  30. symbol_table.add_variable("x",x);
  31. symbol_table.add_constants();
  32. expression_t expression;
  33. expression.register_symbol_table(symbol_table);
  34. parser_t parser;
  35. parser.compile(expression_string,expression);
  36. for (x = T(-5); x <= T(+5); x += T(0.001))
  37. {
  38. const T y = expression.value();
  39. printf("%19.15f\t%19.15f\n", x, y);
  40. }
  41. }
  42. int main()
  43. {
  44. trig_function<double>();
  45. return 0;
  46. }