From 55759bb04c703becf3849a5119966c8d40eb5107 Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Sun, 11 Mar 2018 12:00:26 +1100 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html --- Makefile | 9 +++++---- exprtk.hpp | 18 ++++++++---------- exprtk_simple_example_10.cpp | 35 ++++++++++++++++++----------------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 95939c6..16e0357 100644 --- a/Makefile +++ b/Makefile @@ -21,10 +21,11 @@ OPTIMIZATION_OPT := -O1 BASE_OPTIONS := -pedantic-errors -Wall -Wextra -Werror -Wno-long-long OPTIONS := $(BASE_OPTIONS) $(OPTIMIZATION_OPT) LINKER_OPT := -L/usr/lib -lstdc++ -lm -ASAN_OPT := -g -fsanitize=address -fno-omit-frame-pointer -MSAN_OPT := -g -fsanitize=memory -fno-omit-frame-pointer -LSAN_OPT := -g -fsanitize=leak -fno-omit-frame-pointer -BUILD_SRC := $(wildcard exprtk_*.cpp) +ASAN_OPT := -g -fsanitize=address -fno-omit-frame-pointer +MSAN_OPT := -g -fsanitize=memory -fno-omit-frame-pointer +LSAN_OPT := -g -fsanitize=leak -fno-omit-frame-pointer +USAN_OPT := -g -fsanitize=undefined -fno-omit-frame-pointer +BUILD_SRC := $(sort $(wildcard exprtk_*.cpp)) BUILD_LIST := $(BUILD_SRC:%.cpp=%) diff --git a/exprtk.hpp b/exprtk.hpp index aafb846..6043aa6 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -67,7 +67,7 @@ namespace exprtk #define exprtk_error_location \ "exprtk.hpp:" + details::to_str(__LINE__) \ - #if __GNUC__ >= 7 + #if defined(__GNUC__) && (__GNUC__ >= 7) #define exprtk_disable_fallthrough_begin \ _Pragma ("GCC diagnostic push") \ @@ -1320,8 +1320,8 @@ namespace exprtk template inline T frac_impl(const T v, real_type_tag) { return (v - static_cast(v)); } template inline T trunc_impl(const T v, real_type_tag) { return T(static_cast(v)); } - template inline T const_pi_impl(real_type_tag) { return numeric::constant::pi; } - template inline T const_e_impl (real_type_tag) { return numeric::constant::e; } + template inline T const_pi_impl(real_type_tag) { return T(numeric::constant::pi); } + template inline T const_e_impl (real_type_tag) { return T(numeric::constant::e); } template inline T abs_impl(const T v, int_type_tag) { return ((v >= T(0)) ? v : -v); } template inline T exp_impl(const T v, int_type_tag) { return std::exp (v); } @@ -2780,7 +2780,7 @@ namespace exprtk Note: The following 'awkward' conditional is due to various broken msvc compilers. */ - #if _MSC_VER == 1600 + #if defined(_MSC_VER) && (_MSC_VER == 1600) const bool within_range = !is_end(s_itr_ + 2) && !is_end(s_itr_ + 3) ; #else @@ -3761,7 +3761,7 @@ namespace exprtk case lexer::token::e_sub : return false; case lexer::token::e_colon : return false; case lexer::token::e_ternary : return false; - default : return true; + default : return true ; } } } @@ -3775,7 +3775,7 @@ namespace exprtk case lexer::token::e_eof : return false; case lexer::token::e_colon : return false; case lexer::token::e_ternary : return false; - default : return true; + default : return true ; } } else if (details::is_left_bracket(static_cast(t))) @@ -5963,10 +5963,8 @@ namespace exprtk else return ((T(2) * arg1 <= (arg2 + arg0)) ? arg0 : arg2); - default : { - exprtk_debug(("trinary_node::value() - Error: Invalid operation\n")); - return std::numeric_limits::quiet_NaN(); - } + default : exprtk_debug(("trinary_node::value() - Error: Invalid operation\n")); + return std::numeric_limits::quiet_NaN(); } } diff --git a/exprtk_simple_example_10.cpp b/exprtk_simple_example_10.cpp index 1f5b910..6ce237b 100644 --- a/exprtk_simple_example_10.cpp +++ b/exprtk_simple_example_10.cpp @@ -45,22 +45,23 @@ void newton_sqrt() .add( function_t( // define function: newton_sqrt(x) "newton_sqrt", - " switch " - " { " - " case x < 0 : -inf; " - " case x == 0 : 0; " - " case x == 1 : 1; " - " default: " - " ~{ " - " var z := 100; " - " var sqrt_x := x / 2; " - " repeat " - " sqrt_x := (1 / 2) * (sqrt_x + (x / sqrt_x)); " - " if (equal(sqrt_x^2, x)) " - " break[sqrt_x]; " - " until ((z -= 1) <= 0); " - " }; " - " } ", + " switch " + " { " + " case x < 0 : null; " + " case x == 0 : 0; " + " case x == 1 : 1; " + " default: " + " ~{ " + " var z := 100; " + " var sqrt_x := x / 2; " + " repeat " + " if (equal(sqrt_x^2, x)) " + " break[sqrt_x]; " + " else " + " sqrt_x := (1 / 2) * (sqrt_x + (x / sqrt_x)); " + " until ((z -= 1) <= 0); " + " }; " + " } ", "x")); std::string expression_str = "newton_sqrt(x)"; @@ -77,7 +78,7 @@ void newton_sqrt() T result = expression.value(); - printf("sqrt(%03d) - Result: %12.10f\tReal: %12.10f\n", + printf("sqrt(%03d) - Result: %15.13f\tReal: %15.13f\n", static_cast(i), result, std::sqrt(x));