From 0d8601550fd98bff40c6d96855aea1119cb82dd3 Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Thu, 24 May 2012 06:03:26 +1000 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html --- exprtk.hpp | 39 ++++++++++++++++++++++++++++++++------- exprtk_test.cpp | 10 ++++++---- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/exprtk.hpp b/exprtk.hpp index a907870..57d6844 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -22,7 +22,7 @@ * (07) z:=x+sin(2*pi/y) * * (08) u:=2*(pi*z)/(w:=x+cos(y/pi)) * * (09) clamp(-1,sin(2*pi*x)+cos(y/2*pi),+1) * - * (10) inrange(-2,m,+2)==if(({-2<=m}and[m<=+2]),1,0) * + * (10) inrange(-2,m,+2)==if(({-2<=m} and [m<=+2]),1,0) * * (11) (12.34sin(x)cos(2y)7+1)==(12.34*sin(x)*cos(2*y)*7+1) * * (12) (x ilike 's*ri?g') and [y<(3z^7+w)] * * * @@ -1945,6 +1945,18 @@ namespace exprtk } }; + template + inline bool is_true(const expression_node* node) + { + return (T(0) != node->value()); + } + + template + inline bool is_false(const expression_node* node) + { + return (T(0) == node->value()); + } + template inline bool is_unary_node(const expression_node* node) { @@ -1981,6 +1993,22 @@ namespace exprtk return !is_variable_node(node); } + template + class null_node : public expression_node + { + public: + + inline T value() const + { + return std::numeric_limits::quiet_NaN(); + } + + inline typename expression_node::node_type type() const + { + return expression_node::e_nul; + } + }; + template class literal_node : public expression_node { @@ -2450,7 +2478,7 @@ namespace exprtk inline T value() const { - if (test_->value() != 0) + if (is_true(test_)) return consequent_->value(); else return alternative_->value(); @@ -2495,7 +2523,7 @@ namespace exprtk inline T value() const { T result = T(0); - while (test_->value() != T(0)) + while (is_true(test_)) { result = branch_->value(); } @@ -5351,10 +5379,7 @@ namespace exprtk inline T value() const { - if (expression_holder_ && expression_holder_->expr) - return expression_holder_->expr->value(); - else - return std::numeric_limits::quiet_NaN(); + return expression_holder_->expr->value(); } inline T operator()() const diff --git a/exprtk_test.cpp b/exprtk_test.cpp index 13fbaa5..0011162 100644 --- a/exprtk_test.cpp +++ b/exprtk_test.cpp @@ -1309,7 +1309,7 @@ inline bool run_test05() symbol_table.add_variable("y_var123",y); symbol_table.add_constants(); - const std::size_t expression_count = 100; + const std::size_t expression_count = 10; for (std::size_t i = 0; i < expression_count; ++i) { expression_t e; @@ -1325,7 +1325,7 @@ inline bool run_test05() } const T pi = T(3.14159265358979323846); - const T increment = T(0.0001); + const T increment = T(0.001); while ((x <= T(+1000.0)) && (y <= T(+1000.0))) { @@ -2165,13 +2165,15 @@ inline bool run_test11() printf("run_test11() - Error in evaluation!(1)\n"); return false; } + expression.release(); - T result2 = expression.value(); - if (result2 == result2) + + if (false == (!expression)) { printf("run_test11() - Error in evaluation!(2)\n"); return false; } + { exprtk::parser parser; if (!parser.compile(expression_string,expression))