Dangl.Calculator 1.0.5

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Dangl.Calculator --version 1.0.5
NuGet\Install-Package Dangl.Calculator -Version 1.0.5
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="Dangl.Calculator" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dangl.Calculator --version 1.0.5
#r "nuget: Dangl.Calculator, 1.0.5"
#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 Dangl.Calculator as a Cake Addin
#addin nuget:?package=Dangl.Calculator&version=1.0.5

// Install Dangl.Calculator as a Cake Tool
#tool nuget:?package=Dangl.Calculator&version=1.0.5

Dangl.Calculator

Build Status NuGet MyGet

Online Documentation

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

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 both netstandard1.1 and net45. 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.

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
'(' 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 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.

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


MIT Licence

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.1 is compatible.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.0 8,337 11/15/2023
2.1.0 9,868 6/30/2023
2.0.0 12,424 1/4/2023
1.5.0 16,708 4/29/2022
1.4.0 18,696 1/23/2021
1.3.1 404 1/16/2021
1.3.0 431 1/15/2021
1.2.1 492 12/26/2020
1.2.0 22,049 8/5/2019
1.1.5 1,007 7/12/2019
1.1.4 1,641 3/8/2019
1.1.3 778 12/28/2018
1.1.2 839 10/19/2018
1.1.1 851 10/9/2018
1.1.0 840 9/15/2018
1.0.6 1,146 3/19/2018
1.0.5 1,740 12/15/2017
1.0.4 1,019 8/11/2017
1.0.3 1,143 12/5/2016
1.0.2 1,233 10/4/2016
1.0.1 1,106 9/25/2016
1.0.0 1,276 9/25/2016

1.0.5: Downgrade to netstandard1.1 and net45 for broader compatibility
   
     1.0.4: Update ANTLR to latest version
     
     1.0.3: Target NETStandard 1.3