Mathematical Expressions (or Mathex for short) is a library for C++ that evaluates mathematical expressions from strings at runtime according to Mathex Specification. It aims to provide fast, easy, customizable and, most importantly, safe evaluation with no dependencies.
The library is completely C++11 standard compliant.
Using Mathex is super easy - just include one header, initialize and evaluate. That's it.
#include <iostream>
#include <mathex>
#include <string>
int main() {
// Use `Config` class and `DefaultFlags` to get default settings.
// For what settings are available, check out documentation.
mathex::Config config(mathex::DefaultFlags);
// Config class contains your settings along with custom
// variables and functions you inserted.
double x = 1.5;
config.addVariable("x", x);
// These variables and functions are then available for users
// to use in expressions.
std::string input = "2x + 5";
double result;
// Mathex writes result of evaluation into the reference you provide.
// Here we pass a reference to a `result` variable we declared above.
if (config.evaluate(input, result) == mathex::Success) {
// Outputs `2x + 5 is 8`
std::cout << input << " is " << result << std::endl;
}
}
Don't forget to link Mathex when you compile your program:
c++ program.cpp -lmathex++
To build the library, you need to clone the repository using Git and build the binary using GNU Make:
git clone https://github.com/capsey/mathex-cpp.git
cd ./mathex-cpp
make # to specify compiler: CXX=clang++ make
It will use default C++ compiler on your system (c++
). If you want to use specific compiler, export environment variable CXX
with your desired compiler before running make
.
After compilation, library binary will be in bin
directory. The header file is located in include
directory.