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
Rad2Deg
Constant factor to convert radians to degrees.
public const double Rad2Deg = 57.29577951308232
Field Value
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
valueTThe value for which to find the ceiling multiple.
stepTThe step. Must be non-zero.
Returns
- T
The smallest multiple of
stepthat is >=value.
Type Parameters
TA numeric type supporting modulus and basic arithmetic operators.
Exceptions
- DivideByZeroException
Thrown if
stepequals 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
valueTValue to clamp.
minTMinimum limit (must be less than or equal to
max).maxTMaximum limit (must be greater than or equal to
min).
Returns
- T
A clamped value between
minandmax.
Type Parameters
TA type that implements IComparable<T>.
Exceptions
- ArgumentException
Thrown if
minis greater thanmax.
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
valueTValue to clamp.
minTMinimum limit (must be less than or equal to
max).maxTMaximum limit (must be greater than or equal to
min).comparerIComparer<T>Custom comparer for determining ordering.
Returns
- T
A clamped value between
minandmax.
Type Parameters
TType to clamp.
Exceptions
- ArgumentException
Thrown if
minis greater thanmax.
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
lineNumberintZero-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
valueTThe value for which to find the floor multiple.
stepTThe step. Must be non-zero.
Returns
- T
The greatest multiple of
stepthat is <=value.
Type Parameters
TA numeric type supporting modulus and basic arithmetic operators.
Exceptions
- DivideByZeroException
Thrown if
stepequals 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
aTFirst value.
bTSecond value.
Returns
- T
The greatest common divisor of
aandb.
Type Parameters
TAn 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
valueTValue to test.
stepTThe divisor. Must be non-zero.
Returns
Type Parameters
TA numeric type supporting modulus, addition, and equality operators.
Exceptions
- DivideByZeroException
Thrown if
stepequals 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
valueTValue to test.
Returns
Type Parameters
TA 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
aTFirst value.
bTSecond value.
Returns
- T
The least common multiple of
aandb.
Type Parameters
TAn 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
aTStart value.
bTEnd value.
tTInterpolation factor.
Returns
- T
The interpolated value
a + t * (b − a).
Type Parameters
TA 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
comparerIComparer<T>An IComparer<T> implementation.
valuesT[]One or more values to compare.
Returns
- T
The maximum value among the input set.
Type Parameters
TType of the elements being compared.
Exceptions
- ArgumentException
Thrown if
valuesis 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
valuesT[]One or more values to compare.
Returns
- T
The maximum value among the input set.
Type Parameters
TA type that implements IComparable<T>.
Exceptions
- ArgumentException
Thrown if
valuesis 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
comparerIComparer<T>An IComparer<T> implementation.
valuesT[]One or more values to compare.
Returns
- T
The minimum value among the input set.
Type Parameters
TType of the elements being compared.
Exceptions
- ArgumentException
Thrown if
valuesis 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
valuesT[]One or more values to compare.
Returns
- T
The minimum value among the input set.
Type Parameters
TA type that implements IComparable<T>.
Exceptions
- ArgumentException
Thrown if
valuesis 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
aTDividend.
bTDivisor. Should be non-zero.
Returns
- T
A non-negative remainder.
Type Parameters
TA 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
valueBigIntegerThe value to round (negative values are handled correctly).
significantDigitsintNumber of significant digits to keep. Must be ≥ 1.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
significantDigitsis 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
valueTValue to be rounded.
exponentintThe 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
TA 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
valueTValue to be rounded.
stepTRounding step. Must be non-zero.
Returns
- T
The value of
valuerounded to the nearest multiple ofstep.
Type Parameters
TA numeric type supporting modulus and comparison operators.
Exceptions
- DivideByZeroException
Thrown if
stepequals zero.