dangl - Dangl.Calculator 2.2.0
Package to evaluate mathematical formulas from strings
PM> Install-Package Dangl.Calculator -Version 2.2.0 -Source https://www.myget.org/F/dangl/api/v3/index.json
> nuget.exe install Dangl.Calculator -Version 2.2.0 -Source https://www.myget.org/F/dangl/api/v3/index.json
> dotnet add package Dangl.Calculator --version 2.2.0 --source https://www.myget.org/F/dangl/api/v3/index.json
source https://www.myget.org/F/dangl/api/v3/index.json
nuget Dangl.Calculator ~> 2.2.0
Copy to clipboard
> choco install Dangl.Calculator --version 2.2.0 --source https://www.myget.org/F/dangl/api/v2
Import-Module PowerShellGet
Register-PSRepository -Name "dangl" -SourceLocation "https://www.myget.org/F/dangl/api/v2"
Install-Module -Name "Dangl.Calculator" -RequiredVersion "2.2.0" -Repository "dangl"
Copy to clipboard
Dangl.Calculator
Online Documentation
Changelog
This calculator is using the ANTLR4 C# target to calculate results from formulas that are passed in as string.
Whenever a calculation is performed, a CalculationResult is returned with the following properties:
| Property | Type | |
|---|---|---|
| IsValid | bool | true if the formula could be parsed and calculated, else false |
| ErrorPosition | int | Position of the offending symbol in the line, 0 based index, for invalid results, else null |
| ErrorMessage | string | ANTLR error message for invalid formulas, else null |
| Result | double | NaN for invalid formulas, else the actual result |
You can find the TypeScript version here: https://github.com/GeorgDangl/antlr-calculator
Installation
Install it via NuGet: Dangl.Calculator
CI builds are available via MyGet.
https://www.myget.org/F/dangl/api/v3/index.json
Compatibility
This project targets netstandard2.0, netstandard1.1, net45 and net40. Due to .Net 4.5.2 being the currently latest supported version
by Microsoft and the xUnit test suite, no tests are run for net45 and net451.
The .NET 4.0 target is for compatibility reasons, it is not tested and requires .NET compilers for version 4.5 or newer to properly function.
Project Configuration
If this project is consumed in a project using the full .Net framework with a newer version of
Antlr4.Runtime, the necessary AssemblyBindingRedirects are not automatically generated with the current
dotnet CLI tooling. This is scheduled to be fixed with the 2.0 release. In the meantime, the following should
be added to the consumers csproj:
<PropertyGroup Condition=" '$(TargetFramework)' == 'net461' ">
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
The Condition=" '$(TargetFramework)' == 'net461' " attribute may be changed as necessary or removed.
Example
using Dangl.Calculator;
public void Example()
{
var formula = "5+5";
var calculation = Calculator.Calculate(formula);
Console.WriteLine(calculation.Result);
Console.WriteLine(calculation.IsValid);
// 10.0
// true
}
Supported functions
| Expression | |
|---|---|
FLOOR expression |
Round down to zero accuracy |
CEIL expression |
Round up to zero accuracy |
ABS expression |
Absolute value |
ROUNDK '(' expression ';' expression ')' |
Round expr_1 with expr_2 accuracy |
ROUND expression |
Round with zero accuracy |
TRUNC expression |
Trim decimal digits |
SIN expression |
Sinus |
COS expression |
Cosinus |
TAN expression |
Tangens |
COT expression |
Cotangens |
SINH expression |
Sinus Hypererbolicus |
COSH expression |
Cosinus Hyperbolicus |
TANH expression |
Tangens Hyperbolicus |
ARCSIN expression |
Inverse Sinus |
ARCCOS expression |
Inverse Cosinus |
ARCTAN expression |
Inverse Tangens |
ARCTAN2 '(' expression ';' expression ')' |
Atan2 |
ARCCOT expression |
Inverse Cotangens |
EXP expression |
e ^ expr |
LN expression |
Logarithm to e |
EEX expression |
10 ^ expr |
LOG expression |
Logarithm to 10 |
RAD expression |
Angle to radians (360° base) |
DEG expression |
Radians to angle (360° base) |
SQRT expression |
Square root |
SQR expression |
Square product |
expression op = ('^'\|'**') expression |
expr_1 to the expr_2 th power |
expression (MOD \| '%' ) expression |
Modulo |
expression DIV expression |
Whole part of division rest |
expression op = ('~'\|'//') expression |
expr_1 nth root of expr_2 |
expression op = ('*'\|'/') expression |
Multiplication or division |
expression op = ('+'\|'-') expression |
Addition or subtraction |
NUMBER |
Single integer or float number |
MIN '(' expression (';' expression)* ')' |
Minimum |
MAX '(' expression (';' expression)* ')' |
Maximum |
NUMBER |
Single integer or float number |
'(' expression ')' |
Expression within parentheses |
PI '()'? |
Mathematical constant pi = 3,141593 |
expression E+ expression |
Exponent, e.g. 10e+43 |
expression E- expression |
Inverted Exponent, e.g. 10e-43 |
EULER |
Mathematical constant e = 2,718282 |
'-' expression |
Unary minus sign (negative numbers) |
'+' expression |
Unary plus sign (positive numbers) |
'(' expression ')' expression |
Expressions without multiplication sign, e.g. 2(3) -> 2*(3) |
expression '(' expression ')' |
Expressions without multiplication sign, e.g. 2(3) -> 2*(3) |
expression may be any expression as functions can be nested. Example: DEG(2*PI) or LOG(10^3).
Formulas can be case invariant, e.g. SIN, sin and siN are all considered the same.
An optional equals sign = at the end of the function is allowed.
Comments in Formulas
Comments in Formulas are supported by encapsulating them either in /*...*/, '...' or "..." quote styles. Examples:
4/*Length*/*3/*Width*/ resolves to 12
4'Length'*3'Width' resolves to 12
4"Length"*3"Width" resolves to 12
Substitutions
The calculator can be called with an overload that accepts a callback function for substitution values. For example, take the following formula:
1,2*#Z4+3
Here, #Z4 is a substitution, which is a placeholder that can be externally supplied. Let's say you want to resolve #Z4 to the value three, you could
make this simple call:
var formula = "1,2*#Z4+3";
var result = Calculator.Calculate(formula, substitution =>
{
if (substitution == "#Z4")
{
return 3;
}
return null;
});
The callback is in the form of a Func<string, double?>, and it will be called for every substitution found in the formula. Multiple substitutions are supported.
If duplicates in substitutions are present, the calculator will request each one individually. If a substitution resolves to null, the formula is considered invalid.
Substitutions must always start with the # character and can then have the following characters: [a-z] | [A-Z] | [äÄöÖüÜ] | [0-9]
Ranges
Similar to Substitutions, the calculator also can handle Ranges. They look like two substitutions joined by two points, e.g. #1..#5. A separate range resolver can be supplied to the calculator as a callback function, so that you can plug in your own logic to resolve ranges when being given a Start and End substitution.
Trailing comments
Formulas may be terminated with a semicolon ; at the end, followed by extra input that is not evaluated. This is useful when, instead of regular comments, you
just want to attach some trailing formation at the end of a formula. For example, the following formula:
1 + 3; As per our counting
Would just evaluate the 1 + 3 portion and return a valid result with the value 4, ignoring the trailing semicolon and all input that follows.
Assembly Strong Naming & Usage in Signed Applications
This module produces strong named assemblies when compiled. When consumers of this package require strongly named assemblies, for example when they
themselves are signed, the outputs should work as-is.
The key file to create the strong name is adjacent to the csproj file in the root of the source project. Please note that this does not increase
security or provide tamper-proof binaries, as the key is available in the source code per
Microsoft guidelines
v2.2.0:
- Dropped tests for
net6.0and addes tests fornet8.0 - Added a new overload to the calculator which accepts an options object. Additionally, it is now possible to configure a special case that applies different handling for formulas that start with a negative power, e.g.
-2^2. This would typically be parsed as(-2)^2, but with the new option, it can be configured to be parsed as-(2^2). It is not recommended to use this option, since most other software (e.g. Microsoft Excel and Google Sheets) do also not behave in that way. - Square
[]and curly{}parentheses are now also valid for formulas. They are only used to group expressions, and implicit multiplication like2(3)=6is not allowed with those
v2.1.0:
- Added support for ranges, e.g.
#START..#ENDto implement custom sum logic
v2.0.0:
- The ANTLR dependencies were updated from Antlr.Runtime to Antlr.Runtime.Standard. The new package is the now official ANTLR runtime package and includes many performance improvements
- Compatibility for
net40andnetstandard1.1was dropped, the lowest supported frameworks now are ´net45andnetstandard2.0` - Dropped tests for
net5.0and added tests fornet7.0
v1.5.0:
- Added support for
MinandMaxformulas - Dropped tests for
netcoreapp2.1and added tests fornet5.0andnet6.0
v1.4.0:
- The calculator now supports trailing comments in a formula, separated by a semicolon
;at the end of the actual formula input. For example,1 + 2; Hello World!now just evaluates1 + 2and ignores everything after the semicolon;
v1.3.1:
- Updates to README
v1.3.0:
- Add support for substitutions in formulas
v1.2.1:
- Add
net40as target framework - Drop tests for
netcoreapp2.2and add tests fornetcoreapp3.1
v1.2.0
- The generated assemblies now have a strong name. This is a breaking change of the binary API and will require recompilation on all systems that consume this package. The strong name of the generated assembly allows compatibility with other, signed tools. Please note that this does not increase security or provide tamper-proof binaries, as the key is available in the source code per Microsoft guidelines
v1.1.5:
- CI tests are now also run on Linux
- Added capability to detect missing multiplication signs, e.g. the formula
3piis now recognized as3*pior2(3)as2*(3)
v1.1.4:
- Added support for formulas ending with an equals sign
=
v1.1.3:
- Dropped tests for
netcoreapp2.0, added tests fornetcoreapp2.2 - Update of dependencies
v1.1.2
- Bugfix: Fixed operator precedence for exponential expression, e.g.
1e+2*2 == 200
v1.1.1
- Bugfix: Unary minus signs were not always evaluated to the nearest matching expression, e.g.
-3+5was evaluated as-(3+5). See GitHub Issue #1
v1.1.0
- Dropped test support for .NET Core 1.0 and 1.1
- Updated Antlr4 to latest stable version
v1.0.6
- Add
netstandard2.0target - Switch build system to NUKE
v1.0.5
- Downgrade to netstandard1.1 and net45 for broader compatibility
v1.0.4
- Update ANTLR to latest version
v1.0.3
- Target NETStandard 1.3
-
.NETFramework 4.5
- Antlr4.Runtime.Standard (>= 4.13.1)
-
.NETStandard 2.0
- Antlr4.Runtime.Standard (>= 4.13.1)
- .NETFramework 4.5: 4.5.0.0
- .NETStandard 2.0: 2.0.0.0
OwnersGeorg Dangl |
AuthorsGeorg Dangl |
Project URLhttps://github.com/GeorgDangl/Dangl.Calculator |
LicenseUnknown |
Info285 total downloads |
| 5 downloads for version 2.2.0 |
| Download (66.53 KB) |
| Found on the current feed only |
Package history
| Version | Size | Last updated | Downloads | Mirrored? | |||
|---|---|---|---|---|---|---|---|
|
|
2.2.0 | 66.53 KB | Wed, 15 Nov 2023 23:17:26 GMT | 5 |
|
||
|
|
2.1.0 | 63.97 KB | Fri, 30 Jun 2023 15:18:36 GMT | 5 |
|
||
|
|
2.1.0-beta0003 | 63.98 KB | Fri, 30 Jun 2023 14:19:29 GMT | 4 |
|
||
|
|
2.1.0-beta0002 | 63.94 KB | Fri, 30 Jun 2023 09:45:47 GMT | 5 |
|
||
|
|
2.0.0 | 62.07 KB | Wed, 04 Jan 2023 14:29:00 GMT | 5 |
|
||
|
|
2.0.0-beta0012 | 62.11 KB | Wed, 04 Jan 2023 14:22:09 GMT | 5 |
|
||
|
|
2.0.0-beta0011 | 62.1 KB | Tue, 15 Nov 2022 14:00:43 GMT | 5 |
|
||
|
|
1.5.1-beta0001 | 115.84 KB | Fri, 29 Apr 2022 12:48:39 GMT | 5 |
|
||
|
|
1.5.0 | 115.83 KB | Fri, 29 Apr 2022 12:46:17 GMT | 5 |
|
||
|
|
1.5.0-beta0014 | 115.88 KB | Fri, 29 Apr 2022 12:44:13 GMT | 6 |
|
||
|
|
1.5.0-beta0013 | 115.88 KB | Fri, 29 Apr 2022 12:40:15 GMT | 6 |
|
||
|
|
1.4.1-beta0002 | 111.36 KB | Sat, 23 Jan 2021 20:37:44 GMT | 5 |
|
||
|
|
1.4.0 | 111.29 KB | Sat, 23 Jan 2021 16:01:06 GMT | 6 |
|
||
|
|
1.4.0-beta0002 | 111.35 KB | Sat, 23 Jan 2021 15:58:53 GMT | 5 |
|
||
|
|
1.3.1 | 108.99 KB | Sat, 16 Jan 2021 08:53:26 GMT | 5 |
|
||
|
|
1.3.1-pullrequest0004-0003 | 109.18 KB | Fri, 15 Jan 2021 21:44:13 GMT | 5 |
|
||
|
|
1.3.1-beta0005 | 109.06 KB | Sat, 16 Jan 2021 08:51:30 GMT | 5 |
|
||
|
|
1.3.1-beta0004 | 109.05 KB | Sat, 16 Jan 2021 08:49:24 GMT | 5 |
|
||
|
|
1.3.1-beta0002 | 109.04 KB | Fri, 15 Jan 2021 21:58:51 GMT | 4 |
|
||
|
|
1.3.1-beta0001 | 109.07 KB | Fri, 15 Jan 2021 21:26:02 GMT | 6 |
|
||
|
|
1.3.0 | 109.01 KB | Fri, 15 Jan 2021 21:09:54 GMT | 4 |
|
||
|
|
1.3.0-beta0003 | 109.04 KB | Fri, 15 Jan 2021 21:01:53 GMT | 5 |
|
||
|
|
1.2.1 | 104.44 KB | Sat, 26 Dec 2020 22:41:47 GMT | 5 |
|
||
|
|
1.2.1-beta0005 | 104.5 KB | Sat, 26 Dec 2020 21:25:31 GMT | 4 |
|
||
|
|
1.2.0 | 78.4 KB | Mon, 05 Aug 2019 17:36:36 GMT | 5 |
|
||
|
|
1.2.0-beta0004 | 78.45 KB | Mon, 05 Aug 2019 17:34:03 GMT | 5 |
|
||
|
|
1.1.6-beta0001 | 77.19 KB | Wed, 24 Jul 2019 10:58:13 GMT | 4 |
|
||
|
|
1.1.5 | 77.18 KB | Fri, 12 Jul 2019 21:06:47 GMT | 5 |
|
||
|
|
1.1.5-beta0003 | 77.22 KB | Fri, 12 Jul 2019 20:17:34 GMT | 6 |
|
||
|
|
1.1.5-beta0002 | 77.22 KB | Fri, 12 Jul 2019 19:38:46 GMT | 5 |
|
||
|
|
1.1.4 | 75.9 KB | Fri, 08 Mar 2019 14:18:25 GMT | 4 |
|
||
|
|
1.1.4-beta0008 | 75.93 KB | Fri, 08 Mar 2019 14:15:45 GMT | 5 |
|
||
|
|
1.1.4-beta0001 | 75.58 KB | Fri, 28 Dec 2018 14:00:59 GMT | 5 |
|
||
|
|
1.1.3 | 75.56 KB | Fri, 28 Dec 2018 12:54:00 GMT | 5 |
|
||
|
|
1.1.3-beta0004 | 75.62 KB | Fri, 28 Dec 2018 12:47:37 GMT | 4 |
|
||
|
|
1.1.2 | 75.53 KB | Fri, 19 Oct 2018 19:59:07 GMT | 5 |
|
||
|
|
1.1.2-beta0004 | 75.56 KB | Fri, 19 Oct 2018 19:49:07 GMT | 5 |
|
||
|
|
1.1.2-beta0002 | 75.54 KB | Fri, 19 Oct 2018 17:23:53 GMT | 5 |
|
||
|
|
1.1.1 | 75.42 KB | Tue, 09 Oct 2018 04:21:07 GMT | 5 |
|
||
|
|
1.1.1-beta0001 | 75.45 KB | Tue, 09 Oct 2018 04:16:02 GMT | 5 |
|
||
|
|
1.1.0 | 75.29 KB | Sat, 15 Sep 2018 17:02:57 GMT | 5 |
|
||
|
|
1.0.7-beta0014 | 75.33 KB | Sat, 15 Sep 2018 16:59:55 GMT | 4 |
|
||
|
|
1.0.7-beta0012 | 75.37 KB | Sat, 15 Sep 2018 16:56:55 GMT | 4 |
|
||
|
|
1.0.6 | 60.25 KB | Mon, 19 Mar 2018 21:39:47 GMT | 5 |
|
||
|
|
1.0.6-build-9 | 40.63 KB | Wed, 07 Feb 2018 19:34:52 GMT | 5 |
|
||
|
|
1.0.6-build-8 | 40.62 KB | Thu, 01 Feb 2018 18:06:35 GMT | 6 |
|
||
|
|
1.0.6-build-7 | 40.62 KB | Thu, 04 Jan 2018 20:07:27 GMT | 6 |
|
||
|
|
1.0.6-build-6 | 40.62 KB | Fri, 15 Dec 2017 15:38:25 GMT | 5 |
|
||
|
|
1.0.6-build-10 | 40.63 KB | Wed, 07 Feb 2018 19:36:21 GMT | 6 |
|
||
|
|
1.0.6-beta0011 | 60.29 KB | Mon, 19 Mar 2018 21:29:01 GMT | 5 |
|
||
|
|
1.0.6-beta0010 | 60.29 KB | Mon, 19 Mar 2018 19:49:44 GMT | 4 |
|
||
|
|
1.0.6-beta0009 | 60.27 KB | Mon, 19 Mar 2018 19:33:58 GMT | 5 |
|
||
|
|
1.0.5 | 40.58 KB | Fri, 15 Dec 2017 15:34:38 GMT | 6 |
|
||
|
|
1.0.5-build-4 | 40.63 KB | Fri, 15 Dec 2017 15:28:50 GMT | 5 |
|
||
|
|
1.0.5-build-3 | 40.6 KB | Sun, 12 Nov 2017 22:34:45 GMT | 5 |
|
||
|
|
1.0.5-build-2 | 40.6 KB | Fri, 01 Sep 2017 21:17:32 GMT | 6 |
|
||
|
|
1.0.5-build-1 | 40.59 KB | Fri, 01 Sep 2017 21:13:22 GMT | 5 |
|