C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html

This commit is contained in:
Arash Partow 2012-05-24 06:03:26 +10:00
parent 1b4e3d7cc2
commit 0d8601550f
2 changed files with 38 additions and 11 deletions

View File

@ -1945,6 +1945,18 @@ namespace exprtk
} }
}; };
template <typename T>
inline bool is_true(const expression_node<T>* node)
{
return (T(0) != node->value());
}
template <typename T>
inline bool is_false(const expression_node<T>* node)
{
return (T(0) == node->value());
}
template <typename T> template <typename T>
inline bool is_unary_node(const expression_node<T>* node) inline bool is_unary_node(const expression_node<T>* node)
{ {
@ -1981,6 +1993,22 @@ namespace exprtk
return !is_variable_node(node); return !is_variable_node(node);
} }
template <typename T>
class null_node : public expression_node<T>
{
public:
inline T value() const
{
return std::numeric_limits<T>::quiet_NaN();
}
inline typename expression_node<T>::node_type type() const
{
return expression_node<T>::e_nul;
}
};
template <typename T> template <typename T>
class literal_node : public expression_node<T> class literal_node : public expression_node<T>
{ {
@ -2450,7 +2478,7 @@ namespace exprtk
inline T value() const inline T value() const
{ {
if (test_->value() != 0) if (is_true(test_))
return consequent_->value(); return consequent_->value();
else else
return alternative_->value(); return alternative_->value();
@ -2495,7 +2523,7 @@ namespace exprtk
inline T value() const inline T value() const
{ {
T result = T(0); T result = T(0);
while (test_->value() != T(0)) while (is_true(test_))
{ {
result = branch_->value(); result = branch_->value();
} }
@ -5351,10 +5379,7 @@ namespace exprtk
inline T value() const inline T value() const
{ {
if (expression_holder_ && expression_holder_->expr)
return expression_holder_->expr->value(); return expression_holder_->expr->value();
else
return std::numeric_limits<T>::quiet_NaN();
} }
inline T operator()() const inline T operator()() const

View File

@ -1309,7 +1309,7 @@ inline bool run_test05()
symbol_table.add_variable("y_var123",y); symbol_table.add_variable("y_var123",y);
symbol_table.add_constants(); 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) for (std::size_t i = 0; i < expression_count; ++i)
{ {
expression_t e; expression_t e;
@ -1325,7 +1325,7 @@ inline bool run_test05()
} }
const T pi = T(3.14159265358979323846); 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))) 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"); printf("run_test11() - Error in evaluation!(1)\n");
return false; return false;
} }
expression.release(); expression.release();
T result2 = expression.value();
if (result2 == result2) if (false == (!expression))
{ {
printf("run_test11() - Error in evaluation!(2)\n"); printf("run_test11() - Error in evaluation!(2)\n");
return false; return false;
} }
{ {
exprtk::parser<T> parser; exprtk::parser<T> parser;
if (!parser.compile(expression_string,expression)) if (!parser.compile(expression_string,expression))