exprtk_simple_example_14.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. **************************************************************
  3. * C++ Mathematical Expression Toolkit Library *
  4. * *
  5. * Simple Example 14 *
  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 stddev_example()
  22. {
  23. typedef exprtk::expression<T> expression_t;
  24. typedef exprtk::parser<T> parser_t;
  25. const std::string stddev_program =
  26. " var x[25] := { "
  27. " 1, 2, 3, 4, 5, "
  28. " 6, 7, 8, 9, 10, "
  29. " 11, 12, 13, 14, 15, "
  30. " 16, 17, 18, 19, 20, "
  31. " 21, 22, 23, 24, 25 "
  32. " }; "
  33. " "
  34. " sqrt(sum([x - avg(x)]^2) / x[]) ";
  35. expression_t expression;
  36. parser_t parser;
  37. parser.compile(stddev_program,expression);
  38. const T stddev = expression.value();
  39. printf("stddev(1..25) = %10.6f\n",stddev);
  40. }
  41. int main()
  42. {
  43. stddev_example<double>();
  44. return 0;
  45. }