-
Notifications
You must be signed in to change notification settings - Fork 5
Home
This library aims to provide a simple API for solving mathematical expressions. These must be specified in a string using Infix notation. Internally the code uses an AST-based algorithm to calculate the result. This project includes a REPL command line utility named xeval.
- Source Inclusion. The project is entirely written in C# and is also thought to be included in another (C#) project.
- DLL Referencing. Anyway you can reference the library as binary DLL file, consuming it from any .NET language.
It follows a basic sample:
using ExpressionEngine;
// ...
string infix = "21 + ((-3 * .4) + 3 ^ 2 * sqrt(301))";
Expression expression = Expression.Create(infix);
try {
double result = (double) expression.Value; //175,944164156077
} catch (ExpressionException e) {
Console.WriteLine("LINE {0}; ERR: {1}", e.ColumnNumber, e.Message);
}
For a more advanced use, please refer to this and other unit tests.
Unary operators: plus (+), minus (-)
Binary operators: addition (+), subtraction (-), multiplication (*), division (/), modulo (%), exponent (^)
Parentheses: open '(', close ')'
Literals: Integer [+|-]0-9, Real [+|-][0-9].0-9
Built-Ins:
- pi: ratio of a circle's circumference to its diameter.
- e: natural logarithmic base.
- sqrt(x): square root.
- cos(x): cosine.
- sin(x): sine.
- tan(x): tangent.
And more; a complete list will be available as soon. Until that moment please refer to source code.
Please remember that is an Alpha version. Public API can change very frequently until release of first Beta. I also discourage using it in production level software.