What is the best way to evaluate any custom math expression, for example
3+sqrt(5)+pow(3)+log(5)
I know that embedding Python into C++ can do that; is there any better way?
Thanks!
What is the best way to evaluate any custom math expression, for example
3+sqrt(5)+pow(3)+log(5)
I know that embedding Python into C++ can do that; is there any better way?
Thanks!
While searching a library for a similar task I found libmatheval. Seems to be a proper thing. Unfortunately, GPL, which is unacceptable for me.
Not sure why 'pow' only has one parameter, but using the ExprTk library one can derive the following simple solution:
I have developed a simple expression parser in C++ and Java. At the moment they only handle arithmetic operators +. -, / * but there is no reason they could not be extended to accommodate more functions.
These simple examples use the shunting yard algorithm to convert the expressions into reverse Polish notation and then another simple stack-based algorithm to actually evaulate the expression.
Code samples can be found here.
I've written a simple, easy-to-use, front-end to Lua for evaluating arithmetic expressions from C (and C++ of course). See http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#ae . See also OpenSouce C/C++ Math expression parser Library and What is a fast C or Objective-C math parser?
Boost.Spirit is a C++ parser library.
Examples: