info.lundin.math.dll
1.2.6
Math expression parser, evaluates a string mathematical expression and returns a double value.
InstallPackage info.lundin.math.dll Version 1.2.6
dotnet add package info.lundin.math.dll version 1.2.6
<PackageReference Include="info.lundin.math.dll" Version="1.2.6" />
paket add info.lundin.math.dll version 1.2.6
#r "nuget: info.lundin.math.dll, 1.2.6"
// Install info.lundin.math.dll as a Cake Addin
#addin nuget:?package=info.lundin.math.dll&version=1.2.6
// Install info.lundin.math.dll as a Cake Tool
#tool nuget:?package=info.lundin.math.dll&version=1.2.6
info.lundin.math  ExpressionParser
Math expression parser, evaluates a string mathematical expression and returns a double value.
More information at http://lundin.info/mathparser
Author: Patrik Lundin patrik@lundin.info
Quick use
ExpressionParser parser = new ExpressionParser();
parser.Values.Add("x", 2);
parser.Values.Add("y", 10);
double result = parser.Parse("x^3+5x^23");
Changing variable values
ExpressionParser parser = new ExpressionParser();
// Create value instances
DoubleValue xval = new DoubleValue();
DoubleValue yval = new DoubleValue();
// Add values for variables x and y
parser.Values.Add("x", xval);
parser.Values.Add("y", yval);
xval.Value = 2; // Update value of "x"
yval.Value = 10; // Update value of "y"
double result = parser.Parse("x^3+5x^23");
Updating values using the SetValue method:
parser.Values["x"].SetValue(2); // x previously added with Add
parser.Values["y"].SetValue(10); // y previously added with Add
Updating by casting and setting the Value property:
((DoubleValue)parser.Values["x"]).Value = 2; // x previously added with Add
((DoubleValue)parser.Values["y"]).Value = 10; // y previously added with Add
Faster evaluation by keeping the expression tree
ExpressionParser parser = new ExpressionParser();
parser.Values.Add("x", 2);
parser.Values.Add("y", 10);
// Parse once
string func = "x^3+5x^23";
parser.Parse(func);
// Fetch expression
Expression expression = parser.Expressions[func];
// Evaluate saved expression
double result = parser.EvalExpression(expression);
Serialization of parsed expressions
To save some initial parsing time you can serialize the expressions to a Stream for storage and later use.
Both the Expression (see above) and the ExpressionDictionary accessable by the property parser.Expressions
have Save and Load methods that take a Stream as parameter. Serializing should only be needed with large
sets of expressions where the initial parse time may be significant.
Please note that serializing expressions and then deserializing using a different culture setting may
have unintended effects. If you do use the serialization feature it is recommended to leave the culture
setting to the default invariant culture.
Supported operators and functions
+, , *, /, ^, %
^ is raised to (power) for example 3^2
% is the modulo operator
sqrt, sin, cos, tan, atan, acos, asin, acotan, exp, ln, log, sinh, cosh, tanh, abs, ceil, floor,
fac, sfac, round, fpart
These functions mostly map to the System.Math functions except fac, sfac which is the factorial
and semifactorial functions and fpart which returns the decimal part of a value.
!, ==, !=, , &&, >, < , >=, ⇐
Logical operators, 1.0 means true, 0.0 means false. If an expression evaluates to anything other
than 1.0 it is considered false.
There is currently no support for adding additional operators or functions.
Supported constants
PI (value of System.Math.PI)
Euler (value of System.Math.E) true (1.0)
false (0.0)
infinity (value of Double.PositiveInfinity)
Variable naming restrictions
All variable names must start with an alphabetic letter (az) and may contain digits at the end but not
inside the variable name.
Examples: x, y, z, var1, var2, myverylongvariablename
Variable names can contain function keywords ONLY when ImplicitMultiplication is set to false and RequireParentheses is set to true.
Examples: glucose, sincosexp
Properties and settings
The following properties can be set on the ExpressionParser:
RequireParentheses  toggles the requirement to use parentheses around function arguments (default true)
ImplicitMultiplication  toggles if implicit multiplication is allowed (such as 3x instead of 3*x) (default true)
Culture  the culture to use when parsing, affects decimal and groupign separators (default CurltureInfo.InvariantCulture)
Expressions  provides access to the ExpressionDictionary containing all parsed expressions.
Values  provides access to the ValuesDictionary for adding variable/value pairs to use for evaluating expressions.
Cultures and decimal separators
The default culture is set to CultureInfo.InvariantCulture which uses a single dot (.)
as the decimal separator and comma (,) as grouping (thousands) separator.
You can change the culture by setting the property Culture in the parser, if you do change the
culture the number decimal separator in your expressions must match the culture. It is recommended
that you use the default invariant culture and use a dot as a decimal separator especially if
you decide to save parsed expressions as serialized data.
Culture fa, faIR uses the division operator (/) as the decimal separator, this is not allowed
in the parser and the recommended solution is to use the invariant culture.
Currency symbols are not allowed. Thousands separators are supported but not recommended to use.
Cultures using the same decimal and grouping separators are not allowed.
Limitations of using double values
If you use the parser for financial calculations please make sure you understand how IEEE754 floating
point values work and the rounding errors that may result.
It is generally not recommended to use float or double values for money applications since these are
floating point values with a binary representation, for languages that have them a decimal representation
is recommended, however this parser uses the default System.Math library and only operate with double values.
Unit testing
The provided tests should not be considered to fully test every scenario.
Remember it is YOUR responsibility to verify that the parser works correctly for the intended use,
it is recommended that you write some additional unit tests that tests with your specific data.
License and disclaimer
Patrik Lundin, patrik@lundin.info, http://www.lundin.info
Copyright 20022019 Patrik Lundin
Library and Source code released under the Microsoft Public License (MsPL)
https://opensource.org/licenses/mspl.html
info.lundin.math  ExpressionParser
Math expression parser, evaluates a string mathematical expression and returns a double value.
More information at http://lundin.info/mathparser
Author: Patrik Lundin patrik@lundin.info
Quick use
ExpressionParser parser = new ExpressionParser();
parser.Values.Add("x", 2);
parser.Values.Add("y", 10);
double result = parser.Parse("x^3+5x^23");
Changing variable values
ExpressionParser parser = new ExpressionParser();
// Create value instances
DoubleValue xval = new DoubleValue();
DoubleValue yval = new DoubleValue();
// Add values for variables x and y
parser.Values.Add("x", xval);
parser.Values.Add("y", yval);
xval.Value = 2; // Update value of "x"
yval.Value = 10; // Update value of "y"
double result = parser.Parse("x^3+5x^23");
Updating values using the SetValue method:
parser.Values["x"].SetValue(2); // x previously added with Add
parser.Values["y"].SetValue(10); // y previously added with Add
Updating by casting and setting the Value property:
((DoubleValue)parser.Values["x"]).Value = 2; // x previously added with Add
((DoubleValue)parser.Values["y"]).Value = 10; // y previously added with Add
Faster evaluation by keeping the expression tree
ExpressionParser parser = new ExpressionParser();
parser.Values.Add("x", 2);
parser.Values.Add("y", 10);
// Parse once
string func = "x^3+5x^23";
parser.Parse(func);
// Fetch expression
Expression expression = parser.Expressions[func];
// Evaluate saved expression
double result = parser.EvalExpression(expression);
Serialization of parsed expressions
To save some initial parsing time you can serialize the expressions to a Stream for storage and later use.
Both the Expression (see above) and the ExpressionDictionary accessable by the property parser.Expressions
have Save and Load methods that take a Stream as parameter. Serializing should only be needed with large
sets of expressions where the initial parse time may be significant.
Please note that serializing expressions and then deserializing using a different culture setting may
have unintended effects. If you do use the serialization feature it is recommended to leave the culture
setting to the default invariant culture.
Supported operators and functions
+, , *, /, ^, %
^ is raised to (power) for example 3^2
% is the modulo operator
sqrt, sin, cos, tan, atan, acos, asin, acotan, exp, ln, log, sinh, cosh, tanh, abs, ceil, floor,
fac, sfac, round, fpart
These functions mostly map to the System.Math functions except fac, sfac which is the factorial
and semifactorial functions and fpart which returns the decimal part of a value.
!, ==, !=, , &&, >, < , >=, ⇐
Logical operators, 1.0 means true, 0.0 means false. If an expression evaluates to anything other
than 1.0 it is considered false.
There is currently no support for adding additional operators or functions.
Supported constants
PI (value of System.Math.PI)
Euler (value of System.Math.E) true (1.0)
false (0.0)
infinity (value of Double.PositiveInfinity)
Variable naming restrictions
All variable names must start with an alphabetic letter (az) and may contain digits at the end but not
inside the variable name.
Examples: x, y, z, var1, var2, myverylongvariablename
Variable names can contain function keywords ONLY when ImplicitMultiplication is set to false and RequireParentheses is set to true.
Examples: glucose, sincosexp
Properties and settings
The following properties can be set on the ExpressionParser:
RequireParentheses  toggles the requirement to use parentheses around function arguments (default true)
ImplicitMultiplication  toggles if implicit multiplication is allowed (such as 3x instead of 3*x) (default true)
Culture  the culture to use when parsing, affects decimal and groupign separators (default CurltureInfo.InvariantCulture)
Expressions  provides access to the ExpressionDictionary containing all parsed expressions.
Values  provides access to the ValuesDictionary for adding variable/value pairs to use for evaluating expressions.
Cultures and decimal separators
The default culture is set to CultureInfo.InvariantCulture which uses a single dot (.)
as the decimal separator and comma (,) as grouping (thousands) separator.
You can change the culture by setting the property Culture in the parser, if you do change the
culture the number decimal separator in your expressions must match the culture. It is recommended
that you use the default invariant culture and use a dot as a decimal separator especially if
you decide to save parsed expressions as serialized data.
Culture fa, faIR uses the division operator (/) as the decimal separator, this is not allowed
in the parser and the recommended solution is to use the invariant culture.
Currency symbols are not allowed. Thousands separators are supported but not recommended to use.
Cultures using the same decimal and grouping separators are not allowed.
Limitations of using double values
If you use the parser for financial calculations please make sure you understand how IEEE754 floating
point values work and the rounding errors that may result.
It is generally not recommended to use float or double values for money applications since these are
floating point values with a binary representation, for languages that have them a decimal representation
is recommended, however this parser uses the default System.Math library and only operate with double values.
Unit testing
The provided tests should not be considered to fully test every scenario.
Remember it is YOUR responsibility to verify that the parser works correctly for the intended use,
it is recommended that you write some additional unit tests that tests with your specific data.
License and disclaimer
Patrik Lundin, patrik@lundin.info, http://www.lundin.info
Copyright 20022019 Patrik Lundin
Library and Source code released under the Microsoft Public License (MsPL)
https://opensource.org/licenses/mspl.html
Release Notes
Fixed issue with scientific exponent notation
Dependencies

.NETFramework 4.0
 No dependencies.

.NETStandard 2.0
 No dependencies.
Used By
NuGet packages (5)
Showing the top 5 NuGet packages that depend on info.lundin.math.dll:
Package  Downloads 

Myra
UI Library for MonoGame, FNA and Stride


da.metervue.net
Used internally in MeterVue by I+P Services


Myra.Xenko
UI Library for MonoGame, FNA and Xenko


Myra.Stride
UI Library for MonoGame, FNA and Stride


Myra.PlatformAgnostic
UI Library for MonoGame, FNA and Stride

GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on info.lundin.math.dll:
Repository  Stars 

rds1983/Myra
UI Library for MonoGame, FNA and Stride

Version History
Version  Downloads  Last updated 

1.2.6  4,599  2/20/2019 