https://github.com/ntdls/ntdls.expressionparser
Expression parser for .net. Supports expression nesting, custom variables, custom functions as well as a ton of built in functions.
https://github.com/ntdls/ntdls.expressionparser
library mathematics nuget
Last synced: 5 months ago
JSON representation
Expression parser for .net. Supports expression nesting, custom variables, custom functions as well as a ton of built in functions.
- Host: GitHub
- URL: https://github.com/ntdls/ntdls.expressionparser
- Owner: NTDLS
- License: mit
- Created: 2023-10-13T20:45:49.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-11-13T23:38:29.000Z (8 months ago)
- Last Synced: 2025-11-14T01:16:23.812Z (8 months ago)
- Topics: library, mathematics, nuget
- Language: C#
- Homepage: https://networkdls.com/Entity/ntdls-expressionparser
- Size: 148 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NTDLS.ExpressionParser
📦 Be sure to check out the NuGet package: https://www.nuget.org/packages/NTDLS.ExpressionParser
ExpressionParser is a mathematics parsing engine for .net. It supports expression nesting, custom variables, custom functions all standard mathematical operations for integer, decimal (floating point), logic and bitwise.
🔥 Benchmarked at ~0.22 µs per expression (≈ 4.5M eval/s per core). That’s ~22M arithmetic ops/sec with our test expression.
Roughly on par with compiled expression trees / LLVM-JIT math engines — this parser isn’t "fast for C#"; it’s *fast, period*.
It addition to the custom functions and variables, out of the box it supports: abs, acos, asin, atan, atan2, avg, ceil, clamp, cos, cosh, count, deg, e, exp, floor, hypot, if, log, log10, logn, modpow, not, pi, pow, prod, rad, rand, round, sign, sin, sinh, sqrt, sum, tan, tanh, trunc.
[](https://github.com/NTDLS/NTDLS.ExpressionParser/actions/workflows/Regression%20Tests.yml)
👀 If you came for the C++ version you can find it at: https://github.com/NTDLS/CMathParser
## Basic usage:
>**Simple Example:**
>
>In this example we simply call the static function Expression.Evaluate to compute the string expression.
```csharp
var result = Expression.Evaluate("10 * ((1000 / 5 + (10 * 11)))");
Console.WriteLine($"{result:n2}");
```
>**Simple Example (with work):**
>
>In this example we simply call the static function Expression.Evaluate to compute the string expression, and we also supply an output parameter for which the parser will use to explain the operations.
```csharp
var result = Expression.Evaluate("10 * ((1000 / 5 + (10 * 11))), out var explanation");
Console.WriteLine($"{result:n2}");
Console.WriteLine(explanation);
```
>**Advanced Example:**
>
>In this example we will create an expression that uses two built in functions "Ceil" and "Sum", a custom function called "DoStuff" and one variable called "extra".
```csharp
var expression = new Expression("10 * ((5 + extra + DoStuff(11,55) + ( 10 + !0 )) * Ceil(SUM(11.6, 12.5, 14.7, 11.11)) + 60.5) * 10");
//Add a value for the variable called "extra".
expression.AddParameter("extra", 1000);
//Handler for the custom function:
expression.AddFunction("DoStuff", (double[] parameters) =>
{
double sum = 0;
foreach (var parameter in parameters)
{
sum += parameter;
}
return sum;
});
var result = ExpressionParser.Evaluate(expression);
Console.WriteLine($"{result:n2}");
```
## License
[MIT](https://choosealicense.com/licenses/mit/)