2011-02-25 10:01:35 8 Comments

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!

### Related Questions

#### Sponsored Content

#### 14 Answered Questions

### [SOLVED] What is the effect of extern "C" in C++?

**2009-06-25 02:10:07****Litherum****751987**View**1563**Score**14**Answer- Tags: c++ c linkage name-mangling extern-c

#### 22 Answered Questions

### [SOLVED] What is the "-->" operator in C++?

**2009-10-29 06:57:45****GManNickG****768102**View**8747**Score**22**Answer- Tags: c++ c operators code-formatting standards-compliance

#### 39 Answered Questions

#### 8 Answered Questions

### [SOLVED] C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

**2011-06-11 23:30:14****Nawaz****215989**View**1854**Score**8**Answer- Tags: c++ multithreading c++11 language-lawyer memory-model

#### 26 Answered Questions

### [SOLVED] What is the easiest way to initialize a std::vector with hardcoded elements?

**2010-02-10 10:55:33****Agnel Kurian****800698**View**592**Score**26**Answer- Tags: c++ vector stl initialization

#### 27 Answered Questions

### [SOLVED] Easiest way to convert int to string in C++

**2011-04-08 04:19:41****Nemo****2818488**View**1526**Score**27**Answer- Tags: c++ string int type-conversion

#### 6 Answered Questions

### [SOLVED] What is the difference between 'typedef' and 'using' in C++11?

**2012-05-25 02:39:51****Klaim****257281**View**861**Score**6**Answer- Tags: c++ c++11 typedef using-declaration

## 11 comments

## @wildart 2012-03-01 06:33:21

muParserX is another C++ mathematical expression parser.

## @templatetypedef 2011-02-25 10:06:06

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!

## @a CVn 2011-02-25 10:33:19

Unrelated, but thanks for the tip about Programming Abstractions in C++!

## @yas777 2016-08-25 20:15:04

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:

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

## @user5374653 2015-09-29 20:26:57

Not sure why

'pow'only has one parameter, but using theExprTklibrary one can derive the followingsimplesolution:## @Yury 2013-12-03 13:43:39

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

## @peastman 2013-04-30 22:54:27

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).

## @AndyUK 2012-02-23 19:41:37

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.

## @Rhys Ulerich 2011-06-26 20:24:43

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

## @aaz 2011-02-25 11:00:48

Boost.Spirit is a C++ parser library.

Examples:

## @Tristram Gräbener 2011-02-25 12:27:15

+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)

## @aaz 2011-02-25 12:52:47

@Tristram – You're right. I linked to the current version from the distribution, but the other two links apply to the classic version only.

## @lhf 2011-02-25 12:06:42

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?

## @Maxim Egorushkin 2011-02-25 12:23:25

Format a string like this:

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.## @aaz 2011-02-25 18:04:16

Why cast the result to string?

## @Maxim Egorushkin 2011-02-25 18:07:28

You need to cast it to something because the type of the result the expression yields is unknown, string or a double.