At the core of the generic arithmetic framework is a set of interfaces
that define arithmetic operations on generic types. A type that implements
one or more of these interfaces is an Arithmetic.
An arithmetic performs arithmetic operations on a specific operand type.
The relationship between an airthmetic type and its operand type is
declared using a custom attribute.
The arithmetic operations are grouped according to the algebraic structure
of the operand type.
Addition and Subtraction: Groups
A group is a set that has an addition operator.
It usually also has a subtraction operator.
The IGroupOperationsT
interface defines the operations on instances of a group.
It depends on IEqualityComparerTIComparerT.
In addition, it defines the following properties and methods:
Member | Description |
---|
MaxValue | The smallest value of the type. |
MinValue | The smallest value of the type. |
Zero | The identity element for addition. |
Abs | Returns the magnitude of an operand. |
Add | Returns the result of adding two operands. |
Negate | Returns the additive inverse of its operand. |
Subtract | Returns the result of subtracting one operand from another. |
A ring is a group that
has a multiplication operator. It usually also has a unit element.
The IRingOperationsT
interface defines the operations on instances of a ring.
It depends on IGroupOperationsT.
In addition, it defines the following properties and methods:
Member | Description |
---|
FromInt32 | Converts an integer to the operand type. |
Multiply | Returns the result of multiplying two operands. |
One | Returns the unit element. |
ScaleByPowerOfTwo | Returns the result of multiplying an operand by a power of two. |
Division presents somewhat of a challenge because it is not always well defined.
For example, the value of 7/2 is not an integer, but it is a rational number.
In technical terms, the set of integers is not closed under division,
but the set of rational numbers is. In other words, the set of rational numbers is the
closure of the integers under division. It is this relationship
that is expressed by the
IDivisionOperationsT, TClosure
interface.
This interface has two type parameters. The first is the operand type. The second
is the type of the closure.
It depends on IRingOperationsT.
In addition, it defines the following methods:
Member | Description |
---|
Divide | Returns the result of dividing two operands as the closure type. |
Reciprocal | Returns the multiplicative inverse of an operand as the closure type. |
A ring that is closed under division is called a field.
Examples of fields are: the rational numbers, the real numbers, and integers modulo
a prime number.
Operations on fields are defined by the
IFieldOperationsT
interface. It depends on
IDivisionOperationsT, TClosure
with the closure type equal to the operand type. It defines no additional members.
Integer Division: Euclidean Rings
A Euclidean ring is a ring that has a well-defined division
with remainder. The name derives from the fact that division with remainder
is the core of Euclid's algorithm to compute the greatest common divisor of two numbers.
The quotient, q and the remainder, r of the
division a / b are uniquely defined such that a = qb + r with
0 <= |r| < b. There are other ways to uniquely define q
and r, which can be specified by the rounding mode.
Division with remainder is defined by the
IEuclideanRingOperationsT
interface.
It depends on IRingOperationsT.
In addition, it defines the following methods:
Member | Description |
---|
DivideWithRemainder | Returns the quotient of dividing two operands and returns the remainder in an out
parameter. |
IntegerDivide | Returns the quotient of dividing two operands. |
IntegerDivide | Returns the quotient of dividing two operands using the specified rounding mode. |
Remainder | Returns the remainder after dividing one operand by another. |
The
IFractionalOperationsT
interface defines methods for rounding numbers.
It depends on IFieldOperationsT.
In addition, it defines the following methods:
Member | Description |
---|
Ceiling | Returns the smallest integer greater than or equal to the operand. |
Floor | Returns the largest integer smaller than or equal to the operand. |
Round | Returns the operand rounded to the specified number of decimal digits
using the specified rounding mode. |
The
IRealOperationsT
interface defines properties and methods involving real numbers, including a large
number of transcendental functions like the ones defined for
Double in the
Math static class.
It depends on IFractionalOperationsT.
In addition, it defines the following methods:
Member | Description |
---|
Epsilon | Gets the machine precision of the operand type. |
MinPositiveValue | Gets the smallest positive number that is greater than zero. |
Pi | Gets the number π. |
FromDouble | Returns the equivalent of a
Double as an instance
of the operand type. |
Atan2 | Returns the inverse tangent of the two operands. |
Cos | Returns the cosine of the operand. |
Cosh | Returns the hyperbolic cosine of the operand. |
Cosh | Returns the hyperbolic cosine of the operand. |
Exp | Returns the exponential of the operand. |
Log | Returns the logarithm of the operand. |
Pow | Returns an operand raised to a power. |
Sin | Returns the sine of the operand. |
Sinh | Returns the hyperbolic sine of the operand. |
Sqrt | Returns the square root of the operand. |
The IEEE-754 standard defines three special values: positive infinity, negative
infinity, and Not-a-Number (NaN). Although these values originally applied to
single and double precision floating-point numbers, they can be used more generally.
The Not-a-Number value in particular deserves special attention.
It is used when the result of an operation is undefined. For example: 0/0
is undefined. NaN's compare not equal to all values, including other NaN's.
The
IIeeeOperationsT
interface defines the properties and methods that support IEEE arithmetic.
It defines the following methods:
Member | Description |
---|
NegativeInfinity | Gets the operand type value that represents Not-a-Number. |
PositiveInfinity | Gets the operand type value that represents positive infinity. |
NegativeInfinity | Gets the operand type value that represents negative infinity. |
IsNaN | Returns whether a value is Not-a-Number. |