Table of Contents

Class MathEx

Namespace
Utils.Mathematics
Assembly
Utils.dll

Provides extended mathematical functions and utilities, including custom rounding, clamping, and Pascal's triangle generation.

public static class MathEx
Inheritance
MathEx
Inherited Members

Fields

Deg2Rad

Constant factor to convert degrees to radians.

public const double Deg2Rad = 0.017453292519943295

Field Value

double

Rad2Deg

Constant factor to convert radians to degrees.

public const double Rad2Deg = 57.29577951308232

Field Value

double

Methods

Ceiling<T>(T, T)

Computes the smallest multiple of step that is greater than or equal to value.

public static T Ceiling<T>(T value, T step) where T : struct, IModulusOperators<T, T, T>, INumberBase<T>

Parameters

value T

The value for which to find the ceiling multiple.

step T

The step. Must be non-zero.

Returns

T

The smallest multiple of step that is >= value.

Type Parameters

T

A numeric type supporting modulus and basic arithmetic operators.

Exceptions

DivideByZeroException

Thrown if step equals zero.

Clamp<T>(T, T, T)

Clamps value between min and max. Returns min if value is less than min, and max if value is greater than max.

public static T Clamp<T>(this T value, T min, T max) where T : IComparable<T>

Parameters

value T

Value to clamp.

min T

Minimum limit (must be less than or equal to max).

max T

Maximum limit (must be greater than or equal to min).

Returns

T

A clamped value between min and max.

Type Parameters

T

A type that implements IComparable<T>.

Exceptions

ArgumentException

Thrown if min is greater than max.

Clamp<T>(T, T, T, IComparer<T>)

Clamps value between min and max, using a custom comparer. Returns min if value is less than min, and max if value is greater than max.

public static T Clamp<T>(this T value, T min, T max, IComparer<T> comparer)

Parameters

value T

Value to clamp.

min T

Minimum limit (must be less than or equal to max).

max T

Maximum limit (must be greater than or equal to min).

comparer IComparer<T>

Custom comparer for determining ordering.

Returns

T

A clamped value between min and max.

Type Parameters

T

Type to clamp.

Exceptions

ArgumentException

Thrown if min is greater than max.

ComputePascalTriangleLine(int)

Computes and returns the specified line of Pascal's triangle, zero-based. Uses a cached dictionary for performance. If the line is not in the cache, it is calculated and then stored.

public static int[] ComputePascalTriangleLine(int lineNumber)

Parameters

lineNumber int

Zero-based index of the line to compute. Must be >= 0.

Returns

int[]

An array representing the requested line of Pascal's triangle.

Remarks

The function updates the cache dynamically up to lineNumber.

Floor<T>(T, T)

Computes the greatest multiple of step that is less than or equal to value.

public static T Floor<T>(T value, T step) where T : struct, IModulusOperators<T, T, T>, INumberBase<T>

Parameters

value T

The value for which to find the floor multiple.

step T

The step. Must be non-zero.

Returns

T

The greatest multiple of step that is <= value.

Type Parameters

T

A numeric type supporting modulus and basic arithmetic operators.

Exceptions

DivideByZeroException

Thrown if step equals zero.

Gcd<T>(T, T)

Computes the greatest common divisor of a and b using the Euclidean algorithm. Always returns a non-negative value. Returns zero when both arguments are zero.

public static T Gcd<T>(T a, T b) where T : struct, IBinaryInteger<T>

Parameters

a T

First value.

b T

Second value.

Returns

T

The greatest common divisor of a and b.

Type Parameters

T

An integer type supporting modulus and absolute-value operations.

IsMultipleOf<T>(T, T)

Returns true if value is an exact multiple of step, i.e., Mod(value, step) == 0.

public static bool IsMultipleOf<T>(T value, T step) where T : struct, INumber<T>

Parameters

value T

Value to test.

step T

The divisor. Must be non-zero.

Returns

bool

true if value is divisible by step.

Type Parameters

T

A numeric type supporting modulus, addition, and equality operators.

Exceptions

DivideByZeroException

Thrown if step equals zero.

IsPowerOfTwo<T>(T)

Returns true if value is a positive power of two (1, 2, 4, 8, …).

public static bool IsPowerOfTwo<T>(T value) where T : struct, IBinaryInteger<T>

Parameters

value T

Value to test.

Returns

bool

true if value is a positive integer of the form 2n.

Type Parameters

T

A binary integer type.

Lcm<T>(T, T)

Computes the least common multiple of a and b. Returns zero if either argument is zero.

public static T Lcm<T>(T a, T b) where T : struct, IBinaryInteger<T>

Parameters

a T

First value.

b T

Second value.

Returns

T

The least common multiple of a and b.

Type Parameters

T

An integer type supporting modulus and absolute-value operations.

Lerp<T>(T, T, T)

Linearly interpolates between a and b by factor t. When t is 0 the result equals a; when 1, it equals b. Values of t outside [0, 1] extrapolate beyond the segment.

public static T Lerp<T>(T a, T b, T t) where T : struct, IFloatingPoint<T>

Parameters

a T

Start value.

b T

End value.

t T

Interpolation factor.

Returns

T

The interpolated value a + t * (b − a).

Type Parameters

T

A floating-point type.

Max<T>(IComparer<T>, params T[])

Returns the maximum value from the specified values, using a custom comparer.

public static T Max<T>(IComparer<T> comparer, params T[] values)

Parameters

comparer IComparer<T>

An IComparer<T> implementation.

values T[]

One or more values to compare.

Returns

T

The maximum value among the input set.

Type Parameters

T

Type of the elements being compared.

Exceptions

ArgumentException

Thrown if values is empty.

Max<T>(params T[])

Returns the maximum value from the specified values.

public static T Max<T>(params T[] values) where T : IComparable<T>

Parameters

values T[]

One or more values to compare.

Returns

T

The maximum value among the input set.

Type Parameters

T

A type that implements IComparable<T>.

Exceptions

ArgumentException

Thrown if values is empty.

Min<T>(IComparer<T>, params T[])

Returns the minimum value from the specified values, using a custom comparer.

public static T Min<T>(IComparer<T> comparer, params T[] values)

Parameters

comparer IComparer<T>

An IComparer<T> implementation.

values T[]

One or more values to compare.

Returns

T

The minimum value among the input set.

Type Parameters

T

Type of the elements being compared.

Exceptions

ArgumentException

Thrown if values is empty.

Min<T>(params T[])

Returns the minimum value from the specified values.

public static T Min<T>(params T[] values) where T : IComparable<T>

Parameters

values T[]

One or more values to compare.

Returns

T

The minimum value among the input set.

Type Parameters

T

A type that implements IComparable<T>.

Exceptions

ArgumentException

Thrown if values is empty.

Mod<T>(T, T)

Computes a mathematical modulo that always returns a non-negative result, unlike the built-in '%' operator which can yield negative remainders. The result is always in the range [0, b).

-1 % 3 = -1
Mod(-1, 3) = 2
public static T Mod<T>(T a, T b) where T : struct, IModulusOperators<T, T, T>, IAdditionOperators<T, T, T>

Parameters

a T

Dividend.

b T

Divisor. Should be non-zero.

Returns

T

A non-negative remainder.

Type Parameters

T

A numeric type supporting modulus and addition operators.

RoundToSignificantDigits(BigInteger, int)

Rounds value to significantDigits most significant digits using standard rounding (≥ 5 rounds up, < 5 rounds down). Returns value unchanged when it has fewer or equal digits than requested.

public static BigInteger RoundToSignificantDigits(BigInteger value, int significantDigits)

Parameters

value BigInteger

The value to round (negative values are handled correctly).

significantDigits int

Number of significant digits to keep. Must be ≥ 1.

Returns

BigInteger

Exceptions

ArgumentOutOfRangeException

Thrown when significantDigits is less than 1.

Round<T>(T, int)

Rounds value to the nearest power-of-ten multiple, specified by exponent. For instance, Round(1234, 2) rounds to the nearest multiple of 100.

public static T Round<T>(T value, int exponent = 0) where T : struct, INumber<T>, IPowerFunctions<T>

Parameters

value T

Value to be rounded.

exponent int

The exponent of 10 used as the rounding step. Defaults to 0 (which rounds to integer).

Returns

T

A value rounded to the nearest power-of-ten multiple.

Type Parameters

T

A numeric type that supports exponentiation via IPowerFunctions<TSelf>.

Round<T>(T, T)

Rounds value to the nearest multiple of step. If value is exactly between two multiples, it is rounded up.

public static T Round<T>(T value, T step) where T : struct, INumber<T>

Parameters

value T

Value to be rounded.

step T

Rounding step. Must be non-zero.

Returns

T

The value of value rounded to the nearest multiple of step.

Type Parameters

T

A numeric type supporting modulus and comparison operators.

Exceptions

DivideByZeroException

Thrown if step equals zero.