⚡ ILMath
ILMath is a hyper-fast and flexible math expression parser for C# and .NET.
Features
- ⚡ Compiles math expressions to IL code!
- 🔃 Compiled expressions can be reused many times!
- 📦 Supports many math functions and constants (including custom)!
- 🔥 Extremely fast (after compilation)!
- 📚 Easy to use!
Installation
ILMath is available as a NuGet package.
Usage
using ILMath;
// Create a new evaluation context
var context = EvaluationContext.CreateDefault();
// Register a custom function
context.RegisterFunction("myFunction", parameters => parameters[0] + parameters[1]);
// Register a custom variable
context.RegisterVariable("myVariable", 5);
// Create a new evaluator with name "MyFunction" and expression "myFunction(2, myVariable) * 3"
var evaluator = MathEvaluation.CompileExpression("MyFunction", "myFunction(2, myVariable) * 3");
// Evaluate the expression
var result = evaluator.Invoke(context); // 21
Operators
ILMath supports the following operators.
| Operator | Description |
|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo |
^ | Exponentiation |
Built-ins
ILMath supports many built-in variables and functions. The following table lists all built-in variables and functions.
Variables
| Variable | Description |
|---|
pi | The ratio of a circle's circumference to its diameter |
e | Euler's number |
tau | The ratio of a circle's circumference to its radius |
phi | The golden ratio |
inf | Infinity |
|