Byces.Calculator
2.0.0
.NET 6.0
This package targets .NET 6.0. 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.
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 Byces.Calculator --version 2.0.0
NuGet\Install-Package Byces.Calculator -Version 2.0.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="Byces.Calculator" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Byces.Calculator --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Byces.Calculator, 2.0.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 Byces.Calculator as a Cake Addin #addin nuget:?package=Byces.Calculator&version=2.0.0 // Install Byces.Calculator as a Cake Tool #tool nuget:?package=Byces.Calculator&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Byces.Calculator
A simple .NET calculator that solves string math expressions
Usage example
ICalculator calculator = new CalculatorBuilder().Build();
string expression = "2 + 5 * 3";
var mathResult = calculator.GetDoubleResult(expression);
if (mathResult.IsValid) // true
{
Console.WriteLine(mathResult.Result); // 17
}
else
{
Console.WriteLine(mathResult.ErrorMessage);
}
Building expressions
Before showing some syntax examples, here are some observations:
- Numbers, variables, operators and functions are not case sensitive
- The calculator is not sensitive to whitespace and using it has a low impact on performance
// You can use signs to represent negative and positive numbers
string expressionExample1 = "-5 + +5";
// You can use parentheses to set priorities in the expression
string expressionExample2 = "((2 + 5) * 2) * 3";
// You can also represent numbers like this
string expressionExample3 = "1E+3";
// This is an expression with two operators and two functions
string expressionExample4 = "fact(2 + 3) * fact3";
Available operations
Arithmetic
Operator | Description | Examples |
---|---|---|
Add | Sums two numeric values | x + y or x add y |
Subtract | Subtracts one numerical value from another | x - y or x sub y |
Multiply | Multiplies two values | x * y or x mul y |
Divide | Divide one number by another | x / y or x div y |
Power | Powers one number by another | x ^ y or x pow y |
Root | Calculates the x root of y | x √ y or x rt y |
Modulus | Returns the remainder of a division | x % y or x mod y |
Logical and Comparison
Operator | Description | Examples |
---|---|---|
And | Returns true if both values are true, otherwise false | x && y |
Or | Returns true if any of the values are true | x \|\| y |
Equal | Returns true if the two values have the same value | x == y |
Not Equal | Returns true if the two values are differen | x != y |
Greater | Returns true if the first number is greater than the second | x > y |
Greater Equal | Returns true if the first number is greater than or equal to the second | x >= y |
Less | Returns true if the first number is less than the second | x < y |
Less Equal | Returns true if the first number is less than or equal to the second | x <= y |
Special
Operator | Description | Examples |
---|---|---|
Semicolon | Does not return any value, it can only be used in functions to pass multiple parameters | fun(x; y) |
Available functions
Arithmetic
Function | Description | Examples | Parameter limit |
---|---|---|---|
Add | Sum the numerical values | add(x; y; ...) |
Min: 1 | Max: Undefined |
Factorial | Calculates the factorial of a number | fact(x) |
Min: 1 | Max: 1 |
Min | Returns the smallest value passed into the function | min(x; y; ...) |
Min: 1 | Max: Undefined |
Max | Returns the largest value passed into the function | max(x; y; ...) |
Min: 1 | Max: Undefined |
Square root | Returns the square root of x | √x or sqrt(x) |
Min: 1 | Max: 1 |
Cube root | Returns the cube root of x | cbrt(x) |
Min: 1 | Max: 1 |
Logarithm (base y) | Calculates the logarithm of x (base y) | log(x; y) |
Min: 2 | Max: 2 |
Logarithm (base 10) | Calculates the logarithm of x (base 10) | log(x) |
Min: 1 | Max: 1 |
Logic and Comparison
Function | Description | Examples | Parameter limit |
---|---|---|---|
Not | Inverts the boolean value | !x |
Min: 1 | Max: 1 |
Trigonometry
Function | Description | Examples | Parameter limit |
---|---|---|---|
Cosine | Gets the cosine of a number | cos(x) |
Min: 1 | Max: 1 |
Sine | Gets the sine of a number | sin(x) |
Min: 1 | Max: 1 |
Tangent | Gets the tangent of a number | tan(x) |
Min: 1 | Max: 1 |
Cosine Hyperbolic | Gets the cosine hyperbolic of a number | cosh(x) |
Min: 1 | Max: 1 |
Sine Hyperbolic | Gets the sine hyperbolic of a number | sinh(x) |
Min: 1 | Max: 1 |
Tangent Hyperbolic | Gets the tangent hyperbolic of a number | tanh(x) |
Min: 1 | Max: 1 |
Radian | Convert degrees to radians | rad(x) |
Min: 1 | Max: 1 |
Degree | Convert radians to degrees | deg(x) |
Min: 1 | Max: 1 |
Available variables
Number
Variable | Description | Examples |
---|---|---|
Pi | Represents the PI value | π or pi |
Euler | Represents the Euler value | e or euler |
Boolean
Variable | Description | Examples |
---|---|---|
True | Represents the boolean true | true |
False | Represents the boolean false | false |
Public Models
MathResult{T} (readonly struct)
Property | Type | Summary |
---|---|---|
IsValid | bool | Gets the validity of the MathResult instance |
ErrorMessage | string? | Gets the error message of a possible syntax problem |
Error | ResultError | Gets the expression error type |
Result | T | Gets the expression result |
CalculatorBuilder (class)
Method | Type | Summary |
---|---|---|
Build() | Calculator | Builds a new Calculator instance |
Calculator (class) and ICalculator (interface)
Method | Type | Summary |
---|---|---|
GetDoubleResult(string expression) | MathResult<double> | Gets a MathResult<double> calculating the given mathematical expression |
GetBooleanResult(string expression) | MathResult<bool> | Gets a MathResult<bool> calculating the given mathematical expression |
Future goals
- Be able to create your own functions and variables in a simple way
- Add more functions, variables and operators
- Try to increase performance
Feedback and Bugs
If you find any bugs or have any suggestions, feel free to send them 😄 !
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. 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 is compatible. 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 was computed. |
.NET Standard | netstandard2.1 is compatible. |
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.
-
.NETStandard 2.1
- MathNet.Numerics (>= 5.0.0)
- Microsoft.Extensions.ObjectPool (>= 7.0.3)
-
net6.0
- MathNet.Numerics (>= 5.0.0)
- Microsoft.Extensions.ObjectPool (>= 7.0.3)
-
net7.0
- MathNet.Numerics (>= 5.0.0)
- Microsoft.Extensions.ObjectPool (>= 7.0.3)
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 | |
---|---|---|---|
3.3.0 | 144 | 4/11/2024 | |
3.2.0 | 140 | 3/18/2024 | |
3.1.1 | 131 | 2/22/2024 | |
3.1.0 | 133 | 2/21/2024 | |
3.0.1 | 136 | 1/28/2024 | |
3.0.0 | 173 | 9/16/2023 | |
2.0.0 | 236 | 3/10/2023 | |
1.1.4 | 241 | 2/25/2023 | |
1.1.3 | 266 | 2/23/2023 | |
1.1.2 | 314 | 1/28/2023 | |
1.1.1 | 312 | 1/28/2023 | |
1.1.0 | 312 | 1/28/2023 | |
1.0.2 | 315 | 1/24/2023 | |
1.0.1 | 300 | 1/23/2023 | |
1.0.0 | 318 | 1/23/2023 |