Skip to content

OnixLabs.Numerics.GenericMath

matthew edited this page May 26, 2026 · 1 revision

GenericMath Class

Provides generic mathematical functions that operate over any numeric type implementing the relevant generic numeric interfaces from System.Numerics.

Namespace OnixLabs.Numerics
Assembly OnixLabs.Numerics.dll (NuGet)
Inheritance object → GenericMath

Definition

public static class GenericMath

Remarks

GenericMath collects mathematical operations that are not directly available through BCL generic math interfaces, or that need special care to produce correct results across all numeric types.

Key design points:

  • Type-precision arithmetic — Pow<T> and Pow10<T> compute the result directly in T using exponentiation by squaring. Routing through a double intermediary would cap accuracy at ~15–17 decimal digits regardless of the precision of T.
  • Unsigned-safe delta — Delta<T> always subtracts the smaller operand from the larger, so unsigned integer types such as byte and uint do not wrap.
  • BigInteger factorial — Factorial<T> accumulates into a BigInteger counter rather than T to avoid overflow when iterating near T.MaxValue.
  • Tuple convenience — MinMax<T> returns both the minimum and the maximum in a single call.

Add a using static OnixLabs.Numerics.GenericMath; directive to call the members unqualified.

Examples

Computing a delta

using static OnixLabs.Numerics.GenericMath;

byte a = 3;
byte b = 200;
byte diff = Delta(a, b); // 197 — safe for unsigned types

Computing a factorial

using static OnixLabs.Numerics.GenericMath;

BigInteger result = Factorial(10); // 3628800

Raising a BigDecimal to a power

using static OnixLabs.Numerics.GenericMath;

BigDecimal value = BigDecimal.Parse("1.5");
BigDecimal cubed = Pow(value, 3); // 3.375 — computed at BigDecimal precision

Powers of ten

using static OnixLabs.Numerics.GenericMath;

decimal scale = Pow10<decimal>(6); // 1000000

Methods

Method Returns Description
Delta<T>(T, T) T Computes the absolute difference between two values.
Factorial<T>(T) BigInteger Computes the factorial of a non-negative integer value.
IntegerLength<T>(T) int Returns the number of digits in the integral component of a value.
MinMax<T>(T, T) (T Min, T Max) Returns both the minimum and maximum of two values.
Pow<T>(T, int) T Raises a value to a non-negative integer exponent.
Pow10<T>(int) T Raises 10 to a non-negative integer exponent.

Delta<T>(T, T)

public static T Delta<T>(T left, T right) where T : INumber<T>

Computes the delta, or absolute difference, between the specified numbers.

Parameter Type Description
left T The left number from which to compute the delta.
right T The right number from which to compute the delta.
Type Parameter Description
T The underlying INumber<TSelf> type.

Returns — The absolute difference between left and right.

Note The subtraction is ordered so that the smaller operand is always subtracted from the larger. This makes the result correct for unsigned integer types, which would otherwise wrap around when left is less than right.


Factorial<T>(T)

public static BigInteger Factorial<T>(T value) where T : IBinaryInteger<T>

Computes the factorial of the specified IBinaryInteger<TSelf> value.

Parameter Type Description
value T The non-negative integer value for which the factorial will be computed.
Type Parameter Description
T The underlying IBinaryInteger<TSelf> type.

Returns — The factorial of value as a BigInteger.

Exceptions

Exception Condition
ArgumentException value is less than zero.

Note The accumulator and loop counter are both BigInteger, not T, to avoid overflow when iterating near T.MaxValue (for example, a byte counter would wrap from 255 to 0 and loop forever).


IntegerLength<T>(T)

public static int IntegerLength<T>(T value) where T : INumberBase<T>

Obtains the number of digits in the integral component of the specified INumberBase<TSelf> value. Fractional digits and the sign are not counted.

Parameter Type Description
value T The value from which to obtain the length of the integral component.
Type Parameter Description
T The underlying INumberBase<TSelf> type.

Returns — The number of digits in the integral component of value.


MinMax<T>(T, T)

public static (T Min, T Max) MinMax<T>(T left, T right) where T : INumber<T>

Obtains the minimum and maximum values from the specified left and right values in a single call.

Parameter Type Description
left T The left INumber<TSelf> value to compare.
right T The right INumber<TSelf> value to compare.
Type Parameter Description
T The underlying INumber<TSelf> type.

Returns — A (T Min, T Max) tuple containing the lesser and greater of the two values.


Pow<T>(T, int)

public static T Pow<T>(T value, int exponent) where T : INumber<T>

Computes value raised to the power of a non-negative integer exponent using exponentiation by squaring, performing all arithmetic in T.

Parameter Type Description
value T The base value to raise.
exponent int The exponent to which value is raised. Must be greater than or equal to zero.
Type Parameter Description
T The numeric type. Must implement INumber<T>.

Returns — A value of type T equal to value raised to the power of exponent, computed at T's precision.

Exceptions

Exception Condition
ArgumentException exponent is less than zero.

Note Computing in T preserves the full precision of the target type. Using a double intermediary (for example T.CreateChecked(Math.Pow(base, exp))) would cap accuracy at ~15–17 significant decimal digits regardless of how precise T is.


Pow10<T>(int)

public static T Pow10<T>(int exponent) where T : INumber<T>

Computes 10 raised to the power of a non-negative integer exponent using exponentiation by squaring, returning the result as type T.

Parameter Type Description
exponent int The exponent to raise 10 to. Must be greater than or equal to zero.
Type Parameter Description
T The numeric type. Must implement INumber<T>.

Returns — A value of type T equal to 10 raised to the power of exponent.

Exceptions

Exception Condition
ArgumentException exponent is less than zero.

See Also

ONIXLabs .NET Library


Clone this wiki locally