Interfaces for Generic Operations
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.
Generic operations
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 IGroupOperations<T> interface defines the operations on instances of a group. It depends on IEqualityComparer<T>IComparer<T>. In addition, it defines the following properties and methods:
Member | Description |
---|---|
The smallest value of the type. | |
The smallest value of the type. | |
The identity element for addition. | |
Returns the magnitude of an operand. | |
Returns the result of adding two operands. | |
Returns the additive inverse of its operand. | |
Returns the result of subtracting one operand from another. |
Multiplication: Rings
A ring is a group that has a multiplication operator. It usually also has a unit element. The IRingOperations<T> interface defines the operations on instances of a ring. It depends on IGroupOperations<T>. In addition, it defines the following properties and methods:
Member | Description |
---|---|
Converts an integer to the operand type. | |
Returns the result of multiplying two operands. | |
Returns the unit element. | |
Returns the result of multiplying an operand by a power of two. |
Division : Fields
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 IDivisionOperations<T, 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 IRingOperations<T>. In addition, it defines the following methods:
Member | Description |
---|---|
Returns the result of dividing two operands as the closure type. | |
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 IFieldOperations<T> interface. It depends on IDivisionOperations<T, 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 IEuclideanRingOperations<T> interface. It depends on IRingOperations<T>. In addition, it defines the following methods:
Member | Description |
---|---|
Returns the quotient of dividing two operands and returns the remainder in an out parameter. | |
Returns the quotient of dividing two operands. | |
Returns the quotient of dividing two operands using the specified rounding mode. | |
Returns the remainder after dividing one operand by another. |
Non-Integers: Rounding
The IFractionalOperations<T> interface defines methods for rounding numbers. It depends on IFieldOperations<T>. In addition, it defines the following methods:
Member | Description |
---|---|
Returns the smallest integer greater than or equal to the operand. | |
Returns the largest integer smaller than or equal to the operand. | |
Returns the operand rounded to the specified number of decimal digits using the specified rounding mode. |
Real numbers
The IRealOperations<T> 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 IFractionalOperations<T>. In addition, it defines the following methods:
Member | Description |
---|---|
Gets the machine precision of the operand type. | |
Gets the smallest positive number that is greater than zero. | |
Gets the number π. | |
Returns the equivalent of a Double as an instance of the operand type. | |
Returns the inverse tangent of the two operands. | |
Returns the cosine of the operand. | |
Returns the hyperbolic cosine of the operand. | |
Returns the hyperbolic cosine of the operand. | |
Returns the exponential of the operand. | |
Returns the logarithm of the operand. | |
Returns an operand raised to a power. | |
Returns the sine of the operand. | |
Returns the hyperbolic sine of the operand. | |
Returns the square root of the operand. |
IEEE arithmetic
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 IIeeeOperations<T> interface defines the properties and methods that support IEEE arithmetic. It defines the following methods:
Member | Description |
---|---|
Gets the operand type value that represents Not-a-Number. | |
Gets the operand type value that represents positive infinity. | |
Gets the operand type value that represents negative infinity. | |
Returns whether a value is Not-a-Number. |