StringMath 4.1.0
.NET 5.0
This package targets .NET 5.0. The package is compatible with this framework or higher.
.NET Core 3.1
This package targets .NET Core 3.1. The package is compatible with this framework or higher.
.NET Standard 2.1
This package targets .NET Standard 2.1. The package is compatible with this framework or higher.
.NET Framework 4.6.1
This package targets .NET Framework 4.6.1. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package StringMath --version 4.1.0
NuGet\Install-Package StringMath -Version 4.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StringMath" Version="4.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StringMath --version 4.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: StringMath, 4.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install StringMath as a Cake Addin #addin nuget:?package=StringMath&version=4.1.0 // Install StringMath as a Cake Tool #tool nuget:?package=StringMath&version=4.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
V3 README can be found here: https://github.com/miroiu/string-math/tree/release-3.0.0
String Math
![alternate text is missing from this package README image](https://img.shields.io/static/v1?label=%20&message=documentation&color=yellow&style=flat-square)
Calculates the value of a math expression from a string returning a double. Supports variables, user defined operators and expression compilation.
double result = "1 * (2 - 3) ^ 2".Eval(); // 1
Variables
double result = "{a} + 2 * {b}".Substitute("a", 2).Substitute("b", 3).Result; // 8
Global variables
These variables are inherited and cannot be substituted.
MathExpr.AddVariable("PI", 3.1415926535897931);
double result = "1 + {PI}".Eval(); // 4.1415926535897931
Custom operators
Global operators
These operators are inherited and can be overidden.
MathExpr.AddOperator("abs", a => a > 0 ? a : -a);
double result = "abs -5".Eval(); // 5
// Operator precedence (you can specify an int for precedence)
MathExpr.AddOperator("max", (a, b) => a > b ? a : b, Precedence.Power);
double result = new MathExpr("2 * 3 max 4").Result; // 8
Local operators
These are applied only to the target expression.
MathExpr expr = "{PI} + 1";
expr.SetOperator("+", (a, b) => Math.Pow(a, b));
double result = expr; // 3.1415926535897931
double result2 = "{PI} + 1".Eval(); // 4.1415926535897931
Advanced
Extract variables
var expr = "{a} + {b} + {PI}".ToMathExpr();
var variables = expr.Variables; // { "a", "b", "PI" }
var localVariables = expr.LocalVariables; // { "a", "b" }
Compilation
Func<double, double> fn = "{a} + 2".ToMathExpr().Compile("a");
double result = fn(5); // 7
Conditional substitution
MathExpr expr = "1 / {a}".Substitute("a", 1);
double temp = expr.Result; // 1
if (someCondition) // true
expr.Substitute("a", 2);
double final = expr.Result; // 0.5
Sharing math context
MathExpr expr = "{PI} + 1";
expr.SetOperator("+", (a, b) => Math.Pow(a, b));
MathExpr expr2 = "3 + 2".ToMathExpr(expr.Context);
double result = "1 + 2 + 3".Eval(expr.Context);
Custom math context
var context = new MathContext(); // new MathContext(MathContext.Default); // to inherit from global
context.RegisterBinary("+", (a, b) => Math.Pow(a, b));
MathExpr expr = new MathExpr("{PI} + 1", context);
MathExpr expr2 = "3 + 2".ToMathExpr(context);
double result = "1 + 2 + 3".Eval(context);
Default operators
Binary
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (remainder)
^ (power)
log (logarithm)
max (maximum)
min (minimum)
Unary
- (negation)
! (factorial)
sqrt (square root)
sin (sine)
asin (arcsine)
cos (cosine)
acos (arccosine)
tan (tangent)
atan (arctangent)
rad (convert degrees to radians)
deg (convert radians to degrees)
ceil (ceiling)
floor (floor)
round (rounding)
exp (e raised to power)
abs (absolute)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 is compatible. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.6.1
- No dependencies.
-
.NETFramework 4.7.2
- No dependencies.
-
.NETFramework 4.8
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on StringMath:
Package | Downloads |
---|---|
KS.CoreCLR
Simulates our future-planned kernel |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on StringMath:
Repository | Stars |
---|---|
miroiu/nodify
Highly performant and modular controls for node-based editors designed for data-binding and MVVM.
|
Added math expression compliation