What is the best way to evaluate mathematical expressions in C++?
Asked Answered
T

11

37

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!

Theall answered 25/2, 2011 at 10:1 Comment(0)
A
3

Boost.Spirit is a C++ parser library.

Examples:

Annapolis answered 25/2, 2011 at 11:0 Comment(2)
+1 for boost.spirit. However your example points to 1.8 which differs considerably with later releases (with parsing now happening in boost::spirit::qi)Limpet
@Tristram – You're right. I linked to the current version from the distribution, but the other two links apply to the classic version only.Annapolis
M
25

Not sure why 'pow' only has one parameter, but using the ExprTk library one can derive the following simple solution:

#include <cstdio>
#include <string>
#include "exprtk.hpp"

int main()
{
   typedef exprtk::expression<double> expression_t;
   typedef exprtk::parser<double>         parser_t;

   std::string expression_string = "3 + sqrt(5) + pow(3,2) + log(5)";

   expression_t expression;

   parser_t parser;

   if (parser.compile(expression_string,expression))
   {
     double result = expression.value();

     printf("Result: %19.15\n",result);
   }
   else
     printf("Error in expression\n.");

   return 0;
}
Multiped answered 29/9, 2015 at 20:26 Comment(0)
Z
4

There is no way to do this with an off-the-shelf standard library in C++, though there are many good parsing algorithms out there that will let you evaluate expressions like these.

If you'd like some references on good parsing algorithms, consider looking into Chapter 14 on expression parsing in Programming Abstractions in C++ (free and available online!), or consider looking into Dijkstra's shunting-yard algorithm. Both of the algorithms mentioned here are simple to implement and will let you evaluate expressions with relative ease.

If you're interested in some more hardcore tools for evaluating expressions, consider looking into the flex and GNU bison tools, which can build powerful parsers for these sorts of expressions. I believe that the bison documentation even shows you how to parse and evaluate arithmetic expressions, so you might have your work already done for you.

Hope this helps!

Zoospore answered 25/2, 2011 at 10:6 Comment(1)
Unrelated, but thanks for the tip about Programming Abstractions in C++!Wont
A
3

Boost.Spirit is a C++ parser library.

Examples:

Annapolis answered 25/2, 2011 at 11:0 Comment(2)
+1 for boost.spirit. However your example points to 1.8 which differs considerably with later releases (with parsing now happening in boost::spirit::qi)Limpet
@Tristram – You're right. I linked to the current version from the distribution, but the other two links apply to the classic version only.Annapolis
B
3

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?

Bewick answered 25/2, 2011 at 12:6 Comment(0)
T
3

muParserX is another C++ mathematical expression parser.

Topsoil answered 1/3, 2012 at 6:33 Comment(0)
O
3

Lepton is another C++ library that can do this. In addition to parsing and evaluating expressions, it also has some more advanced abilities. For example, it can compute analytic derivatives, and it can do some basic algebraic simplification of expressions. The library is quite small, and it's open source (MIT license).

Ottillia answered 30/4, 2013 at 22:54 Comment(0)
A
3

While searching a library for a similar task I found libmatheval. Seems to be a proper thing. Unfortunately, GPL, which is unacceptable for me.

Admixture answered 3/12, 2013 at 13:43 Comment(0)
T
2

The easiest way is to use an external library. The easiest one I've found is TinyExpr. It's written in C, so it should be very easy to call from C++. Also, it's only one source file and one header file. Very easy to integrate. You can get it here.

Solving your example problem is just:

#include "tinyexpr.h"
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Result: %f\n", te_interp("3+sqrt(5)+pow(3,2)+log(5)", 0));
    return 0;
}

I know that embedding Python into C++ can do that

You could do that, but you'd be pulling in a huge dependency to solve a simple problem.

Tuppence answered 25/8, 2016 at 20:15 Comment(0)
P
1

Here's an approach written for recent versions of Boost Spirit: http://agentzlerich.blogspot.com/2011/06/using-boost-spirit-21-to-evaluate.html

Papist answered 26/6, 2011 at 20:24 Comment(0)
T
1

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.

Tam answered 23/2, 2012 at 19:41 Comment(0)
A
-1

Format a string like this:

#include <boost/lexical_cast.hpp>
#include <string>
#include <math.h>

extern "C" {
std::string evaluate() { return boost::lexical_cast<std::string>(3+sqrt(5)+pow(3)+log(5)); }
}

Invoke the C++ compiler to compile the above code into a shared library. Then load that shared library, resolve the address of evaluate, invoke it and get the result.

Amorete answered 25/2, 2011 at 12:23 Comment(2)
Why cast the result to string?Annapolis
You need to cast it to something because the type of the result the expression yields is unknown, string or a double.Amorete

© 2022 - 2024 — McMap. All rights reserved.