Class Calculator
- Namespace
- Cuemon
- Assembly
- Cuemon.Core.dll
Provides a set of static methods for generic arithmetic assignment operations.
public static class Calculator
- Inheritance
-
Calculator
Examples
The following example demonstrates how to use the Calculator class to perform basic arithmetic and bitwise operations. Each method call prints the computed result to the console.
using System;
using Cuemon;
namespace MyApp.Arithmetic;
public class CalculatorExample
{
public void Demonstrate()
{
int sum = Calculator.Add(10, 20);
Console.WriteLine(sum); // 30
int difference = Calculator.Subtract(20, 5);
Console.WriteLine(difference); // 15
int product = Calculator.Multiply(4, 5);
Console.WriteLine(product); // 20
int quotient = Calculator.Divide(20, 4);
Console.WriteLine(quotient); // 5
int remainder = Calculator.Remainder(10, 3);
Console.WriteLine(remainder); // 1
int bitwiseAnd = Calculator.And(0b1100, 0b1010);
Console.WriteLine(bitwiseAnd); // 8 (0b1000)
}
}
Methods
Add<T>(T, T)
Performs a binary addition of the two specified values.
public static T Add<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe first value to add.
yTThe second value to add.
Returns
- T
The sum of
xandy.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.- OverflowException
The sum of
xandyis less than or greater than the byTvalidMinValueandMaxValue.
And<T>(T, T)
Performs a a bitwise logical conjunction (AND) operation of the two specified values.
public static T And<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe first value to AND.
yTThe second value to AND.
Returns
- T
The result of
xANDy.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, short, int, long, sbyte, ushort, uint or ulong.
Assign<T>(T, T)
Performs an assignment of the right-hand operand to the left-hand operand.
public static T Assign<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe left-hand operand.
yTThe right-hand operand.
Returns
- T
The value of
y.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.
Calculate<T>(T, AssignmentOperator, T)
Performs a calculation following the assignment of the two specified values.
public static T Calculate<T>(T x, AssignmentOperator assignment, T y) where T : struct, IConvertible
Parameters
xTThe value to calculate with
y.assignmentAssignmentOperatorOne of the enumeration values that specifies the rules to apply for the assignment operator of
xandy.yTThe value to calculate with
x.
Returns
- T
The result of the
assignmentforxandy.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.
Divide<T>(T, T)
Performs a binary division of the two specified values.
public static T Divide<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe dividend.
yTThe divisor.
Returns
- T
The result of dividing
xbyy.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.- DivideByZeroException
yis zero.- OverflowException
The sum of
xandyis less than or greater than the byTvalidMinValueandMaxValue.
ExclusiveOr<T>(T, T)
Performs a bitwise exclusive or (XOR) operation of the two specified values.
public static T ExclusiveOr<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe first value to XOR.
yTThe second value to XOR.
Returns
- T
The result of
xXORy.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, short, int, long, sbyte, ushort, uint or ulong.
LeftShift<T>(T, T)
Performs an arithmetic left shift (<<) operation.
public static T LeftShift<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe bit pattern to be shifted.
yTThe number of bits to shift the bit pattern.
Returns
- T
The result of shifting the bit pattern.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, short, int, sbyte, ushort.
Multiply<T>(T, T)
Performs a binary multiplication of the two specified values.
public static T Multiply<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe multiplicand.
yTThe multiplier.
Returns
- T
The result of multiplying
xandy.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.- OverflowException
The sum of
xandyis less than or greater than the byTvalidMinValueandMaxValue.
Or<T>(T, T)
Performs a bitwise logical disjunction (OR) operation of the two specified values.
public static T Or<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe first value to OR.
yTThe second value to OR.
Returns
- T
The result of
xORy.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, short, int, long, sbyte, ushort, uint or ulong.
Remainder<T>(T, T)
Performs a binary division of the two specified values and computes the remainder hereof.
public static T Remainder<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe dividend.
yTThe divisor.
Returns
- T
The remainder after dividing
xbyy.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.- DivideByZeroException
yis zero.- OverflowException
The sum of
xandyis less than or greater than the byTvalidMinValueandMaxValue.
RightShift<T>(T, T)
Performs an arithmetic right shift (>>) operation.
public static T RightShift<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe bit pattern to be shifted.
yTThe number of bits to shift the bit pattern.
Returns
- T
The result of shifting the bit pattern.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, short, int, sbyte, ushort.
Subtract<T>(T, T)
Performs a binary subtraction of the two specified values.
public static T Subtract<T>(T x, T y) where T : struct, IConvertible
Parameters
xTThe minuend.
yTThe subtrahend.
Returns
- T
The result of subtracting
yfromx.
Type Parameters
TThe type of the values for the operand operation.
Exceptions
- TypeArgumentException
Tis outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.- OverflowException
The sum of
xandyis less than or greater than the byTvalidMinValueandMaxValue.
ValidAsNumericOperand<T>()
Validates if the specified T is within the allowed range of numeric operands.
public static void ValidAsNumericOperand<T>() where T : struct, IComparable<T>, IEquatable<T>, IConvertible
Type Parameters
TThe type of the value for an operand operation.