From 399a5f9aa3d7db43f9c13361902da8ef3b4c43f5 Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Sun, 28 Jul 2013 21:35:06 +1000 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html --- exprtk.hpp | 128 ++++++++++++++++++----------------- exprtk_benchmark.cpp | 35 +++++----- exprtk_simple_example_02.cpp | 10 +-- exprtk_simple_example_03.cpp | 6 +- exprtk_simple_example_05.cpp | 4 +- exprtk_test.cpp | 46 ++++++------- readme.txt | 94 ++++++++++++------------- 7 files changed, 166 insertions(+), 157 deletions(-) diff --git a/exprtk.hpp b/exprtk.hpp index fd22846..47ea450 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -232,6 +232,12 @@ namespace exprtk return (*this); } + inline build_string& operator << (const char* s) + { + data_ += std::string(s); + return (*this); + } + inline operator std::string () const { return data_; @@ -10216,8 +10222,8 @@ namespace exprtk } if ( - numeric_check_enabled() || - bracket_check_enabled() || + numeric_check_enabled () || + bracket_check_enabled () || sequence_check_enabled() ) { @@ -13564,7 +13570,7 @@ namespace exprtk const details::operator_type& operation, expression_node_ptr (&branch)[2]) { - const Type c = dynamic_cast* >(branch[0])->value(); + const Type c = dynamic_cast*> (branch[0])->value(); const Type& v = dynamic_cast*>(branch[1])->ref(); details::free_node(*(expr_gen.node_allocator_),branch[0]); switch (operation) @@ -13585,7 +13591,7 @@ namespace exprtk expression_node_ptr (&branch)[2]) { const Type& v = dynamic_cast*>(branch[0])->ref(); - const Type c = dynamic_cast* >(branch[1])->value(); + const Type c = dynamic_cast*> (branch[1])->value(); details::free_node(*(expr_gen.node_allocator_),branch[1]); if (expr_gen.cardinal_pow_optimizable(operation,c)) { @@ -13883,7 +13889,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),v0,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -13920,7 +13926,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),v0,v1,c,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -13956,7 +13962,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),v0,v1,c,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -13992,7 +13998,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),v0,c,v1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14028,7 +14034,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),v0,c,v1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14064,7 +14070,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),c,v0,v1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14101,7 +14107,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),c,v0,v1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14137,7 +14143,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),c0,v,c1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14174,7 +14180,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),c0,v,c1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14221,7 +14227,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),c0,c1,v,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14257,7 +14263,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf3ext_expression::template compile(expr_gen,id(expr_gen,o0,o1),v,c0,c1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14313,7 +14319,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14359,7 +14365,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14406,7 +14412,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,c,v2,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14453,7 +14459,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14500,7 +14506,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c,v0,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14547,7 +14553,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14594,7 +14600,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14641,7 +14647,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14688,7 +14694,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,c1,v1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else if (!expr_gen.valid_operator(o1,f1)) return error_node(); @@ -14734,7 +14740,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,v3,f0,f1,f2); @@ -14776,7 +14782,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,c,f0,f1,f2); @@ -14847,7 +14853,7 @@ namespace exprtk typedef typename synthesize_covov_expression1::node_type covov_t; const covov_t* covov = dynamic_cast(branch[1]); const T& v0 = dynamic_cast*>(branch[0])->ref(); - const T& c = covov->t0(); + const T c = covov->t0(); const T& v1 = covov->t1(); const T& v2 = covov->t2(); const details::operator_type o0 = operation; @@ -14860,7 +14866,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,v2,f0,f1,f2); @@ -14888,7 +14894,7 @@ namespace exprtk // c o0 (v0 o1 (v1 o2 v2)) typedef typename synthesize_vovov_expression1::node_type vovov_t; const vovov_t* vovov = dynamic_cast(branch[1]); - const T& c = dynamic_cast*>(branch[0])->value(); + const T c = dynamic_cast*>(branch[0])->value(); const T& v0 = vovov->t0(); const T& v1 = vovov->t1(); const T& v2 = vovov->t2(); @@ -14946,7 +14952,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,c1,v1,f0,f1,f2); @@ -14988,7 +14994,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,v1,c1,f0,f1,f2); @@ -15031,7 +15037,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,v1,c1,f0,f1,f2); @@ -15073,7 +15079,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,c1,v1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,c1,v1,f0,f1,f2); @@ -15115,7 +15121,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,v3,f0,f1,f2); @@ -15157,7 +15163,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,c,f0,f1,f2); @@ -15199,7 +15205,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,c,v2,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,v2,f0,f1,f2); @@ -15241,7 +15247,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,v2,f0,f1,f2); @@ -15284,7 +15290,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c,v0,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,v2,f0,f1,f2); @@ -15327,7 +15333,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,c1,v1,f0,f1,f2); @@ -15356,9 +15362,9 @@ namespace exprtk typedef typename synthesize_covoc_expression0::node_type covoc_t; const covoc_t* covoc = dynamic_cast(branch[1]); const T& v0 = dynamic_cast*>(branch[0])->ref(); - const T& c0 = covoc->t0(); + const T c0 = covoc->t0(); const T& v1 = covoc->t1(); - const T& c1 = covoc->t2(); + const T c1 = covoc->t2(); const details::operator_type o0 = operation; const details::operator_type o1 = expr_gen.get_operator(covoc->f0()); const details::operator_type o2 = expr_gen.get_operator(covoc->f1()); @@ -15369,7 +15375,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,v1,c1,f0,f1,f2); @@ -15412,7 +15418,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o0,f0)) + else if (!expr_gen.valid_operator(o0,f0)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,v1,c1,f0,f1,f2); @@ -15470,7 +15476,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,v3,f0,f1,f2); @@ -15513,7 +15519,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,c,f0,f1,f2); @@ -15555,7 +15561,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,c,v2,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,v2,f0,f1,f2); @@ -15584,7 +15590,7 @@ namespace exprtk typedef typename synthesize_vocov_expression0::node_type vocov_t; const vocov_t* vocov = dynamic_cast(branch[0]); const T& v0 = vocov->t0(); - const T& c = vocov->t1(); + const T c = vocov->t1(); const T& v1 = vocov->t2(); const T& v2 = dynamic_cast*>(branch[1])->ref(); const details::operator_type o0 = expr_gen.get_operator(vocov->f0()); @@ -15597,7 +15603,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,v2,f0,f1,f2); @@ -15639,7 +15645,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c,v0,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,v2,f0,f1,f2); @@ -15681,7 +15687,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,c1,v1,f0,f1,f2); @@ -15724,7 +15730,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,v1,c1,f0,f1,f2); @@ -15767,7 +15773,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,v1,c1,f0,f1,f2); @@ -15809,7 +15815,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,c1,v1,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,c1,v1,f0,f1,f2); @@ -15851,7 +15857,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,v3,f0,f1,f2); @@ -15894,7 +15900,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,c,f0,f1,f2); @@ -15936,7 +15942,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,c,v2,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,v2,f0,f1,f2); @@ -15978,7 +15984,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,v2,f0,f1,f2); @@ -16020,7 +16026,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c,v0,v1,v2,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,v2,f0,f1,f2); @@ -16062,7 +16068,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,c1,v1,f0,f1,f2); @@ -16105,7 +16111,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,v1,c1,f0,f1,f2); @@ -16148,7 +16154,7 @@ namespace exprtk expression_node_ptr result = error_node(); if (synthesize_sf4ext_expression::template compile(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result)) return result; - if (!expr_gen.valid_operator(o2,f2)) + else if (!expr_gen.valid_operator(o2,f2)) return error_node(); else return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,v1,c1,f0,f1,f2); diff --git a/exprtk_benchmark.cpp b/exprtk_benchmark.cpp index 19699b1..cce68ab 100644 --- a/exprtk_benchmark.cpp +++ b/exprtk_benchmark.cpp @@ -37,7 +37,7 @@ const std::string expression_list[] = { "sin(2 * x) + cos(pi / y)", "1 - sin(2 * x) + cos(pi / y)", "sqrt(111.111 - sin(2 * x) + cos(pi / y) / 333.333)", - "(x^2 / sin(2 * pi / y)) -x / 2", + "(x^2 / sin(2 * pi / y)) - x / 2", "x + (cos(y - sin(2 / x * pi)) - sin(x - cos(2 * y / pi))) - y", "clamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)", "max(3.33, min(sqrt(1 - sin(2 * x) + cos(pi / y) / 3), 1.11))", @@ -82,7 +82,7 @@ void run_exprtk_benchmark(T& x, T& y, exprtk::expression& expression, const std::string& expr_string) { - T total = 0.0; + T total = T(0); unsigned int count = 0; exprtk::timer timer; timer.start(); @@ -95,7 +95,7 @@ void run_exprtk_benchmark(T& x, T& y, } } timer.stop(); - if (T(0.0) != total) + if (T(0) != total) printf("[exprtk] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n", timer.time(), count / timer.time(), @@ -109,7 +109,7 @@ template struct native; template void run_native_benchmark(T& x, T& y, NativeFunction f, const std::string& expr_string) { - T total = 0.0; + T total = T(0); unsigned int count = 0; exprtk::timer timer; timer.start(); @@ -122,7 +122,7 @@ void run_native_benchmark(T& x, T& y, NativeFunction f, const std::string& expr_ } } timer.stop(); - if (T(0.0) != total) + if (T(0) != total) printf("[native] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n", timer.time(), count / timer.time(), @@ -173,7 +173,7 @@ struct native static inline T avg(Type x, Type y) { - return (x + y) / T(2.0); + return (x + y) / T(2); } static inline T clamp(const Type l, const Type v, const Type u) @@ -188,12 +188,12 @@ struct native static inline T func01(Type x, Type y) { - return T(2.0) * (y + x); + return T(2) * (y + x); } static inline T func02(Type x, Type y) { - return (T(2.0) * y + T(2.0) * x); + return (T(2) * y + T(2) * x); } static inline T func03(Type x, Type y) @@ -213,7 +213,7 @@ struct native static inline T func06(Type x, Type y) { - return T(1.0) - ((x * y) + (y / x)) - T(3.0); + return T(1) - ((x * y) + (y / x)) - T(3); } static inline T func07(Type x, Type y) @@ -223,27 +223,28 @@ struct native static inline T func08(Type x, Type y) { - return (1.1*pow(x,T(1.0))+2.2*pow(y,T(2.0))-3.3*pow(x,T(3.0))+4.4*pow(y,T(15.0))-5.5*pow(x,T(23.0))+6.6*pow(y,T(55.0))); + using namespace std; + return (T(1.1)*pow(x,T(1))+T(2.2)*pow(y,T(2))-T(3.3)*pow(x,T(3))+T(4.4)*pow(y,T(15))-T(5.5)*pow(x,T(23))+T(6.6)*pow(y,T(55))); } static inline T func09(Type x, Type y) { - return std::sin(T(2.0) * x) + std::cos(pi / y); + return std::sin(T(2) * x) + std::cos(pi / y); } static inline T func10(Type x, Type y) { - return T(1.0) - std::sin(2.0 * x) + std::cos(pi / y); + return T(1) - std::sin(T(2) * x) + std::cos(pi / y); } static inline T func11(Type x, Type y) { - return std::sqrt(T(111.111) - std::sin(T(2.0) * x) + std::cos(pi / y) / T(333.333)); + return std::sqrt(T(111.111) - std::sin(T(2) * x) + std::cos(pi / y) / T(333.333)); } static inline T func12(Type x, Type y) { - return ((x * x) / std::sin(T(2.0) * pi / y)) -x / T(2.0); + return ((x * x) / std::sin(T(2) * pi / y)) - x / T(2); } static inline T func13(Type x, Type y) @@ -253,17 +254,17 @@ struct native static inline T func14(Type x, Type y) { - return clamp(T(-1.0), std::sin(T(2.0) * pi * x) + std::cos(y / T(2.0) * pi), + T(1.0)); + return clamp(T(-1), std::sin(T(2) * pi * x) + std::cos(y / T(2) * pi), + T(1)); } static inline T func15(Type x, Type y) { - return std::max(T(3.33), std::min(sqrt(T(1.0) - std::sin(T(2.0) * x) + std::cos(pi / y) / T(3.0)), T(1.11))); + return std::max(T(3.33), std::min(sqrt(T(1) - std::sin(T(2) * x) + std::cos(pi / y) / T(3)), T(1.11))); } static inline T func16(Type x, Type y) { - return (((y + (x * T(2.2))) <= (x + y + T(1.1))) ? x - y : x * y) + T(2.0) * pi / x; + return (((y + (x * T(2.2))) <= (x + y + T(1.1))) ? x - y : x * y) + T(2) * pi / x; } }; diff --git a/exprtk_simple_example_02.cpp b/exprtk_simple_example_02.cpp index 2ae670c..d6b2958 100644 --- a/exprtk_simple_example_02.cpp +++ b/exprtk_simple_example_02.cpp @@ -33,9 +33,9 @@ void square_wave() " (1/21)*sin(42*pi*f*t)+(1/23)*sin(46*pi*f*t)+" " (1/25)*sin(50*pi*f*t)+(1/27)*sin(54*pi*f*t))"; static const T pi = T(3.14159265358979323846); - T f = pi/10.0; - T t = T(0.0); - T a = T(10.0); + T f = pi / T(10); + T t = T(0); + T a = T(10); exprtk::symbol_table symbol_table; symbol_table.add_variable("f",f); @@ -49,8 +49,8 @@ void square_wave() exprtk::parser parser; parser.compile(expr_string,expression); - const T delta = (4.0*pi)/1000.0; - for (t = -2.0*pi; t <= +2.0*pi; t+=delta) + const T delta = (T(4) * pi) / T(1000); + for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta) { T result = expression.value(); printf("%19.15f\t%19.15f\n",t,result); diff --git a/exprtk_simple_example_03.cpp b/exprtk_simple_example_03.cpp index 3f97659..ad1b193 100644 --- a/exprtk_simple_example_03.cpp +++ b/exprtk_simple_example_03.cpp @@ -25,9 +25,9 @@ template void polynomial() { std::string expression_string = "25x^5 - 35x^4 - 15x^3 + 40x^2 - 15x + 1"; - T r0 = T(0.0); - T r1 = T(1.0); - T x = T(0.0); + T r0 = T(0); + T r1 = T(1); + T x = T(0); exprtk::symbol_table symbol_table; symbol_table.add_variable("x",x); diff --git a/exprtk_simple_example_05.cpp b/exprtk_simple_example_05.cpp index 438deb5..45de3d4 100644 --- a/exprtk_simple_example_05.cpp +++ b/exprtk_simple_example_05.cpp @@ -39,8 +39,8 @@ void custom_function() { typedef exprtk::expression expression_t; std::string expression_string = "myfunc(sin(x*pi),y/2)"; - T x = T(1.0); - T y = T(2.0); + T x = T(1); + T y = T(2); myfunc mf; exprtk::symbol_table symbol_table; diff --git a/exprtk_test.cpp b/exprtk_test.cpp index 5d1d00a..da3e3e7 100644 --- a/exprtk_test.cpp +++ b/exprtk_test.cpp @@ -1008,7 +1008,7 @@ inline bool not_equal_impl(const T& t1, if (t1 != t1) return true; if (t2 != t2) return true; T diff = std::abs(t1 - t2); - T eps_norm = (std::max(T(1.0),std::max(std::abs(t1),std::abs(t2))) * epsilon); + T eps_norm = (std::max(T(1),std::max(std::abs(t1),std::abs(t2))) * epsilon); return diff > eps_norm; } @@ -1505,28 +1505,28 @@ inline bool run_test02() test_ab("'456' == a[r0+2:r1] ","01234567890123456789","",T(1.0)), test_ab("'4567890123456789' == a[r1-r0:] ","01234567890123456789","",T(1.0)), test_ab("'01234' == a[:r1-r0] ","01234567890123456789","",T(1.0)), - test_ab("a[r0: 6] == b " ,"01234567890123456789","23456",T(1.0)), - test_ab("a[2: r1] == b " ,"01234567890123456789","23456",T(1.0)), - test_ab("a[r0:3*2] == b " ,"01234567890123456789","23456",T(1.0)), - test_ab("a[1+1:r1] == b " ,"01234567890123456789","23456",T(1.0)), - test_ab("a[r0: ] == b " ,"01234567890123456789","234567890123456789",T(1.0)), - test_ab("a[: r1] == b " ,"01234567890123456789","0123456",T(1.0)), - test_ab("a[r0:r1] == b " ,"01234567890123456789","23456",T(1.0)), - test_ab("a[r0:r1+2] == b " ,"01234567890123456789","2345678",T(1.0)), - test_ab("a[r0+2:r1] == b " ,"01234567890123456789","456",T(1.0)), - test_ab("a[r1-r0:] == b " ,"01234567890123456789","4567890123456789",T(1.0)), - test_ab("a[:r1-r0] == b " ,"01234567890123456789","01234",T(1.0)), - test_ab("b == a[r0: 6] " ,"01234567890123456789","23456",T(1.0)), - test_ab("b == a[2: r1] " ,"01234567890123456789","23456",T(1.0)), - test_ab("b == a[r0:3*2] " ,"01234567890123456789","23456",T(1.0)), - test_ab("b == a[1+1:r1] " ,"01234567890123456789","23456",T(1.0)), - test_ab("b == a[r0: ] " ,"01234567890123456789","234567890123456789",T(1.0)), - test_ab("b == a[: r1] " ,"01234567890123456789","0123456",T(1.0)), - test_ab("b == a[r0:r1] " ,"01234567890123456789","23456",T(1.0)), - test_ab("b == a[r0:r1+2] " ,"01234567890123456789","2345678",T(1.0)), - test_ab("b == a[r0+2:r1] " ,"01234567890123456789","456",T(1.0)), - test_ab("b == a[r1-r0:] " ,"01234567890123456789","4567890123456789",T(1.0)), - test_ab("b == a[:r1-r0] " ,"01234567890123456789","01234",T(1.0)), + test_ab("a[r0: 6] == b ","01234567890123456789","23456",T(1.0)), + test_ab("a[2: r1] == b ","01234567890123456789","23456",T(1.0)), + test_ab("a[r0:3*2] == b ","01234567890123456789","23456",T(1.0)), + test_ab("a[1+1:r1] == b ","01234567890123456789","23456",T(1.0)), + test_ab("a[r0: ] == b ","01234567890123456789","234567890123456789",T(1.0)), + test_ab("a[: r1] == b ","01234567890123456789","0123456",T(1.0)), + test_ab("a[r0:r1] == b ","01234567890123456789","23456",T(1.0)), + test_ab("a[r0:r1+2] == b ","01234567890123456789","2345678",T(1.0)), + test_ab("a[r0+2:r1] == b ","01234567890123456789","456",T(1.0)), + test_ab("a[r1-r0:] == b ","01234567890123456789","4567890123456789",T(1.0)), + test_ab("a[:r1-r0] == b ","01234567890123456789","01234",T(1.0)), + test_ab("b == a[r0: 6] ","01234567890123456789","23456",T(1.0)), + test_ab("b == a[2: r1] ","01234567890123456789","23456",T(1.0)), + test_ab("b == a[r0:3*2] ","01234567890123456789","23456",T(1.0)), + test_ab("b == a[1+1:r1] ","01234567890123456789","23456",T(1.0)), + test_ab("b == a[r0: ] ","01234567890123456789","234567890123456789",T(1.0)), + test_ab("b == a[: r1] ","01234567890123456789","0123456",T(1.0)), + test_ab("b == a[r0:r1] ","01234567890123456789","23456",T(1.0)), + test_ab("b == a[r0:r1+2] ","01234567890123456789","2345678",T(1.0)), + test_ab("b == a[r0+2:r1] ","01234567890123456789","456",T(1.0)), + test_ab("b == a[r1-r0:] ","01234567890123456789","4567890123456789",T(1.0)), + test_ab("b == a[:r1-r0] ","01234567890123456789","01234",T(1.0)), test_ab("'01234567890123456789'[0:9] == a ","0123456789","",T(1.0)), test_ab("'01234567890123456789'[0:9] == a[:] ","0123456789","",T(1.0)), test_ab("'01234567890123456789'[0:9] == a[0:] ","0123456789","",T(1.0)), diff --git a/readme.txt b/readme.txt index 9a6b2c2..bdd189d 100644 --- a/readme.txt +++ b/readme.txt @@ -134,9 +134,6 @@ include path (e.g: /usr/include/). | := | Assign the value of x to y. (eg: y := x) | | | Where y is a variable type. | +----------+---------------------------------------------------------+ -| <=> | Swap the values of x and y. (eg: x <=> y) | -| | Where both x and y are variables. | -+----------+---------------------------------------------------------+ (1) Equalities & Inequalities +----------+---------------------------------------------------------+ @@ -144,7 +141,7 @@ include path (e.g: /usr/include/). +----------+---------------------------------------------------------+ | == or = | True only if x is strictly equal to y. (eg: x == y) | +----------+---------------------------------------------------------+ -| <> or != | True only if x does not equal y (eg: x <> y or x != y) | +| <> or != | True only if x does not equal y. (eg: x <> y or x != y) | +----------+---------------------------------------------------------+ | < | True only if x is less than y. (eg: x < y) | +----------+---------------------------------------------------------+ @@ -152,7 +149,7 @@ include path (e.g: /usr/include/). +----------+---------------------------------------------------------+ | > | True only if x is greater than y. (eg: x > y) | +----------+---------------------------------------------------------+ -| >= | True only if x greater than or equal to y (eg: x >= y) | +| >= | True only if x greater than or equal to y. (eg: x >= y) | +----------+---------------------------------------------------------+ (2) Boolean Operations @@ -214,29 +211,29 @@ include path (e.g: /usr/include/). +----------+---------------------------------------------------------+ | equal | Equality test between x and y using normalized epsilon | +----------+---------------------------------------------------------+ -| erf | Error function of x | +| erf | Error function of x. | +----------+---------------------------------------------------------+ -| erfc | Complimentary error function of x | +| erfc | Complimentary error function of x. | +----------+---------------------------------------------------------+ -| exp | e to the power of x | +| exp | e to the power of x. | +----------+---------------------------------------------------------+ | expm1 | e to the power of x minus 1, where x is very small. | | | (eg: expm1(x)) | +----------+---------------------------------------------------------+ | floor | Largest integer that is less than or equal to x. | +----------+---------------------------------------------------------+ -| frac | Fractional portion of x | +| frac | Fractional portion of x. | +----------+---------------------------------------------------------+ | hypot | Hypotenuse of x and y (eg: hypot(x,y) = sqrt(x*x + y*y))| +----------+---------------------------------------------------------+ -| log | Natural logarithm of x | +| log | Natural logarithm of x. | +----------+---------------------------------------------------------+ -| log10 | Base 10 logarithm of x | +| log10 | Base 10 logarithm of x. | +----------+---------------------------------------------------------+ | log1p | Natural logarithm of 1 + x, where x is very small. | | | (eg: log1p(x)) | +----------+---------------------------------------------------------+ -| log2 | Base 2 logarithm of x | +| log2 | Base 2 logarithm of x. | +----------+---------------------------------------------------------+ | logn | Base N logarithm of x (eg: logn(1235,8)) | | | where n > 0 and is an integer. | @@ -266,7 +263,7 @@ include path (e.g: /usr/include/). | sum | Sum of all the inputs. | | | (eg: sum(x,y,z,w,u,v,t) == (x + y + z + w + u + v + t)) | +----------+---------------------------------------------------------+ -| trunc | Integer portion of x | +| trunc | Integer portion of x. | +----------+---------------------------------------------------------+ (4) Trigonometry Functions @@ -282,38 +279,38 @@ include path (e.g: /usr/include/). | atan2 | Arc tangent of (x/y) expressed in radians. [-pi,+pi] | | | eg: atan2(x,y) | +----------+---------------------------------------------------------+ -| cos | Cosine of x | +| cos | Cosine of x. | +----------+---------------------------------------------------------+ -| cosh | Hyperbolic cosine of x | +| cosh | Hyperbolic cosine of x. | +----------+---------------------------------------------------------+ -| cot | Cotangent of x | +| cot | Cotangent of x. | +----------+---------------------------------------------------------+ -| csc | Cosecant of x | +| csc | Cosecant of x. | +----------+---------------------------------------------------------+ -| sec | Secant of x | +| sec | Secant of x. | +----------+---------------------------------------------------------+ -| sin | Sine of x | +| sin | Sine of x. | +----------+---------------------------------------------------------+ -| sinh | Hyperbolic sine of x | +| sinh | Hyperbolic sine of x. | +----------+---------------------------------------------------------+ -| tan | Tangent of x | +| tan | Tangent of x. | +----------+---------------------------------------------------------+ -| tanh | Hyperbolic tangent of x | +| tanh | Hyperbolic tangent of x. | +----------+---------------------------------------------------------+ -| deg2rad | Convert x from degrees to radians | +| deg2rad | Convert x from degrees to radians. | +----------+---------------------------------------------------------+ -| deg2grad | Convert x from degrees to gradians | +| deg2grad | Convert x from degrees to gradians. | +----------+---------------------------------------------------------+ -| rad2deg | Convert x from radians to degrees | +| rad2deg | Convert x from radians to degrees. | +----------+---------------------------------------------------------+ -| grad2deg | Convert x from gradians to degrees | +| grad2deg | Convert x from gradians to degrees. | +----------+---------------------------------------------------------+ (5) String Processing +----------+---------------------------------------------------------+ | FUNCTION | DEFINITION | +----------+---------------------------------------------------------+ -| in | True only if x is a substring of y | +| in | True only if x is a substring of y. | | | (eg: x in y or 'abc' in 'abcdefgh') | +----------+---------------------------------------------------------+ | like | True only if the string x matches the pattern y. | @@ -471,59 +468,64 @@ correctly optimize such expressions for a given architecture. (02) Standard mathematical operator precedence is applied (BEDMAS). - (03) Supported user defined types are numeric and string variables + (03) Results of expressions that are deemed as being 'valid' are to + exist within the set of Real numbers. All other results will be + of the value Not-A-Number (NaN). + + (04) Supported user defined types are numeric and string variables and functions. - (04) All variable and function names are case-insensitive + (05) All variable and function names are case-insensitive. - (05) Variable and function names must begin with a letter + (06) Variable and function names must begin with a letter (A-Z or a-z), then can be comprised of any combination of letters, digits and underscores. (eg: x, var1 or power_func99) - (06) Expression lengths and sub-expression lists are limited only by + (07) Expression lengths and sub-expression lists are limited only by storage capacity. - (07) The life-time of objects registered with or created from a - specific symbol-table must span at least the life-time of - expressions generated using that symbol-table, otherwise - the result will be undefined behavior. + (08) The life-time of objects registered with or created from a + specific symbol-table must span at least the life-time of the + compiled expressions which utilize objects, such as variables, + of that symbol-table, otherwise the result will be undefined + behavior. - (08) Equal/Nequal are normalized equality routines, which use + (09) Equal/Nequal are normalized equality routines, which use epsilons of 0.0000000001 and 0.000001 for double and float types respectively. - (09) All trigonometric functions assume radian input unless + (10) All trigonometric functions assume radian input unless stated otherwise. - (10) Expressions may contain white-space characters such as + (11) Expressions may contain white-space characters such as space, tabs, new-lines, control-feed et al. ('\n', '\r', '\t', '\b', '\v', '\f') - (11) Strings may be constructed from any letters, digits or special + (12) Strings may be constructed from any letters, digits or special characters such as (~!@#$%^&*()[]|=+ ,./?<>;:"`~_), and must be enclosed with single-quotes. eg: 'Frankly, my dear, I do not give a damn!' - (12) User defined normal functions can have up to 20 parameters, + (13) User defined normal functions can have up to 20 parameters, where as user defined vararg-functions can have an unlimited number of parameters. - (13) The inbuilt polynomial functions can be at most of degree 12. + (14) The inbuilt polynomial functions can be at most of degree 12. - (14) Where appropriate constant folding optimisations will be + (15) Where appropriate constant folding optimisations will be applied. (eg: The expression '2+(3-(x/y))' becomes '5-(x/y)') - (15) String processing capabilities are available by default. + (16) String processing capabilities are available by default. To turn them off, the following needs to be defined at compile time: exprtk_disable_string_capabilities - (16) Composited functions can call themselves or any other functions + (17) Composited functions can call themselves or any other functions that have been defined prior to their own definition. - (17) Recursive calls made from within composited functions will have + (18) Recursive calls made from within composited functions will have a stack size bound by the stack of the executing architecture. - (18) Expressions may contain any of the following comment styles: + (19) Expressions may contain any of the following comment styles: 1. // .... \n 2. # .... \n 3. /* .... */