This library uses Boost.Spirit (QI for C++11 and X3 for C++14) and Boost.Fusion (and Boost.Phoenix with C++11) to parse and evaluate mathematical expressions.
The examples below use the X3 variant of Boost Matheval.
#include <iostream>
#include "matheval.hpp"
int main() {
std::cout << matheval::parse("1+1") << '\n';
}
Outputs
2
#include <iostream>
#include <map>
#include "matheval.hpp"
int main() {
std::map<std::string,double> symtab;
symtab.emplace(std::make_pair("x", 2));
symtab.emplace(std::make_pair("y", -1));
std::cout << matheval::parse("cbrt(x/2 + sqrt(x**2/4 + y**3/24))",symtab) << '\n';
}
Outputs
1.25548
We can also evaluate an expression multiple times without paying the cost of parsing again.
#include <iostream>
#include <map>
#include "matheval.hpp"
int main() {
matheval::Parser parser;
parser.parse("x + 1");
std::cout << parser.evaluate({std::make_pair("x",1)}) << ' '
<< parser.evaluate({std::make_pair("x",2)}) << '\n';
}
Outputs
2 3
To build Boost Matheval, just follow the usual CMake workflow.
mkdir build
cd build
cmake ..
make # build the library and the examples
make check # build and run the tests
cd build
cmake ..
make doxygen
open doc/doxygen/html/index.html
General:
- C++14 compatible compiler, i.e. GCC >= 4.8, Clang, Visual Studio 2015.
- Boost.Spirit, Boost.Phoenix, and Boost.MathConstants.
- No support for ternary operators (?:).
- No support for complex numbers.
this repository was forked from https://github.com/hmenke/boost_matheval
Changes from the upstream version:
- use C++11 for the QI parser
- QI and X3 parser support the same mathematical functions.
- use C++ exceptions to abort processing if a mathematical function uses an invalid parameter. All exceptions thrown by the matheval library are derived from matheval::exception . See the matheval.hpp header file for details.
- add ifelse() function
- GNU libmatheval is a C library built using the parser generator YACC with about the same scope as Boost Matheval.
- ExprTk is a true monster with almost 40000 lines in a single header file. It implements a complete state machine including things like logical operations, control structures, and even file IO. Compilation time is even longer than with Boost Matheval.
Distributed under the Boost Software License, Version 1.0.