From 7bc2b30e8fb4131d216faed53294151b2c763860 Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Mon, 26 Sep 2016 10:50:06 +1000 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) https://www.partow.net/programming/exprtk/index.html --- exprtk.hpp | 45 ++++++++++++++++++------------------ exprtk_simple_example_05.cpp | 2 +- exprtk_simple_example_06.cpp | 2 +- exprtk_simple_example_08.cpp | 2 +- exprtk_simple_example_10.cpp | 2 +- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/exprtk.hpp b/exprtk.hpp index b251333..c865833 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -793,7 +793,7 @@ namespace exprtk template inline T abs_impl(const T v, real_type_tag) { - return ((v >= T(0)) ? v : -v); + return ((v < T(0)) ? -v : v); } template @@ -11123,27 +11123,28 @@ namespace exprtk bool body_deletable_; }; - #define exprtk_define_unary_op(OpName) \ - template \ - struct OpName##_op \ - { \ - typedef typename functor_t::Type Type; \ - \ - static inline T process(Type v) \ - { \ - return numeric:: OpName (v); \ - } \ - \ - static inline typename expression_node::node_type type() \ - { \ - return expression_node::e_##OpName; \ - } \ - \ - static inline details::operator_type operation() \ - { \ - return details::e_##OpName; \ - } \ - }; \ + #define exprtk_define_unary_op(OpName) \ + template \ + struct OpName##_op \ + { \ + typedef typename functor_t::Type Type; \ + typedef typename expression_node::node_type node_t; \ + \ + static inline T process(Type v) \ + { \ + return numeric:: OpName (v); \ + } \ + \ + static inline node_t type() \ + { \ + return expression_node::e_##OpName; \ + } \ + \ + static inline details::operator_type operation() \ + { \ + return details::e_##OpName; \ + } \ + }; \ exprtk_define_unary_op(abs ) exprtk_define_unary_op(acos ) diff --git a/exprtk_simple_example_05.cpp b/exprtk_simple_example_05.cpp index 08218a1..1002300 100644 --- a/exprtk_simple_example_05.cpp +++ b/exprtk_simple_example_05.cpp @@ -43,7 +43,7 @@ void custom_function() typedef exprtk::expression expression_t; typedef exprtk::parser parser_t; - std::string expression_string = "myfunc(sin(x * pi),y / 2)"; + std::string expression_string = "myfunc(sin(x * pi), y / 2)"; T x = T(1); T y = T(2); diff --git a/exprtk_simple_example_06.cpp b/exprtk_simple_example_06.cpp index 2a482f4..71f0512 100644 --- a/exprtk_simple_example_06.cpp +++ b/exprtk_simple_example_06.cpp @@ -31,7 +31,7 @@ void vector_function() std::string expression_string = " for (var i := 0; i < min(x[],y[],z[]); i += 1) " " { " - " z[i] := 3sin(x[i]) + 2log(y[i]); " + " z[i] := 3sin(x[i]) + 2log(y[i]); " " } "; T x[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5) }; diff --git a/exprtk_simple_example_08.cpp b/exprtk_simple_example_08.cpp index ab80fd5..dd7dc23 100644 --- a/exprtk_simple_example_08.cpp +++ b/exprtk_simple_example_08.cpp @@ -49,7 +49,7 @@ void composite() .add( function_t("g","3*[f(x) + f(y)]","x","y")); // g(x,y) = 3[f(x) + f(y)] - std::string expression_string = "g(1 + f(x),f(y) / 2)"; + std::string expression_string = "g(1 + f(x), f(y) / 2)"; expression_t expression; expression.register_symbol_table(symbol_table); diff --git a/exprtk_simple_example_10.cpp b/exprtk_simple_example_10.cpp index 36487ba..ee53994 100644 --- a/exprtk_simple_example_10.cpp +++ b/exprtk_simple_example_10.cpp @@ -55,7 +55,7 @@ void newton_sqrt() " var y := x / 2; " " repeat " " y := (1 / 2) * (y + (x / y)); " - " if (equal(y * y,x)) " + " if (equal(y * y, x)) " " break[y]; " " until ((z -= 1) <= 0); " " }; "