Skip to content
Brandon Wood edited this page May 29, 2014 · 2 revisions

How to Use dotMath

The following page is focused on understanding how to use dotMath in your application and get up and running in the shortest amount of time.

Example 1: Evaluating a Simple Expression

var compiler = new EquationCompiler("4+3");
double value = compiler.Calculate();

Example 2: Evaluating an Expression With Variables

Evaluation an expressions with variables requires setting the variables' values before performing the Calculate() step.

var compiler = new EquationCompiler("a+b");
compiler.SetVariable("a", 4);
compiler.SetVariable("b", 3);

double value = compiler.Calculate();

Example 3: Adding a User-defined Function

The dotMath library allows the user to define their own functions. In this example, we'll define a simple 'factorial' function.

var compiler = new EquationCompiler("factorial(5)");
compiler.AddFunction("factorial", x => 
                                    {
                                      int factorial = 1;
                                      for (int i = 1; i <= x; i++) 
                                      {
                                        factorial *= i;
                                      }
                                      return factorial;
                                    });

double value = compiler.Calculate();