Table of Contents

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

x T

The first value to add.

y T

The second value to add.

Returns

T

The sum of x and y.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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 x and y is less than or greater than the by T valid MinValue and MaxValue.

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

x T

The first value to AND.

y T

The second value to AND.

Returns

T

The result of x AND y.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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

x T

The left-hand operand.

y T

The right-hand operand.

Returns

T

The value of y.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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

x T

The value to calculate with y.

assignment AssignmentOperator

One of the enumeration values that specifies the rules to apply for the assignment operator of x and y.

y T

The value to calculate with x.

Returns

T

The result of the assignment for x and y.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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

x T

The dividend.

y T

The divisor.

Returns

T

The result of dividing x by y.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.

DivideByZeroException

y is zero.

OverflowException

The sum of x and y is less than or greater than the by T valid MinValue and MaxValue.

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

x T

The first value to XOR.

y T

The second value to XOR.

Returns

T

The result of x XOR y.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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

x T

The bit pattern to be shifted.

y T

The number of bits to shift the bit pattern.

Returns

T

The result of shifting the bit pattern.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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

x T

The multiplicand.

y T

The multiplier.

Returns

T

The result of multiplying x and y.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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 x and y is less than or greater than the by T valid MinValue and MaxValue.

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

x T

The first value to OR.

y T

The second value to OR.

Returns

T

The result of x OR y.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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

x T

The dividend.

y T

The divisor.

Returns

T

The remainder after dividing x by y.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.

DivideByZeroException

y is zero.

OverflowException

The sum of x and y is less than or greater than the by T valid MinValue and MaxValue.

RightShift<T>(T, T)

Performs an arithmetic right shift (>>) operation.

public static T RightShift<T>(T x, T y) where T : struct, IConvertible

Parameters

x T

The bit pattern to be shifted.

y T

The number of bits to shift the bit pattern.

Returns

T

The result of shifting the bit pattern.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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

x T

The minuend.

y T

The subtrahend.

Returns

T

The result of subtracting y from x.

Type Parameters

T

The type of the values for the operand operation.

Exceptions

TypeArgumentException

T is 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 x and y is less than or greater than the by T valid MinValue and MaxValue.

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

T

The type of the value for an operand operation.

Exceptions

TypeArgumentOutOfRangeException

T is outside the range of allowed types.
Allowed types are: byte, decimal, double, short, int, long, sbyte, float, ushort, uint or ulong.