Mathematical expression interpreter for real-valued input. Works only with real numbers and returns real numbers. Uses the Shunting Yard Algorithm.
-
Have the mathematical expression you want to solve stored in a string in infix notation
e.g.
std::string expr = "-12.4 + exp(sin(rad(68))) * log10(96)"; -
Create a
MathInterpreterobject and callinit_with_expr()initialize the interpreter with the given input expression.e.g.
MathInterpreter inter; inter.init_with_expr(expr); -
Call
calculate()to calculate the expression and save it in a double.e.g.
double result = inter.calculate();
-
Have the mathematical expression you want to solve stored in a string in infix notation. Use dollar sign($) before and after variable names to mark variables.
e.g.
std::string expr = "-12.4 + exp(sin(rad($x$))) * log10($y$)"; //x and y are variables. -
Create a
MathInterpreterobject and callinit_with_expr()initialize the interpreter with the given input expression.e.g.
MathInterpreter inter; inter.init_with_expr(expr); -
Use
set_value()to set values for each registered variable.e.g.
inter.set_value("x", 12.75); inter.set_value("y", 3.12); -
Call
calculate()to calculate the expression and save it in a double.e.g.
double result = inter.calculate();
- Function names can be all lowercase or all uppercase.
- Pi is recognized automatically when entered as a variable.
e.g.
sin(2*$pi$*5) or sin(2*$PI$*5)
- Supported operators: +, -, *, /, %, ^
- Supported functions: Given under enum FUNCTION
- Only real numbers are supported
- Results are returned only as double