Class MathUtils
Contains Math related static methods.
public static class MathUtils
- Inheritance
-
MathUtils
Fields
- AreCloseOverrideD
Optional override for double-precision floating-point equality comparison. If set, this delegate will be used by AreClose(double, double) instead of the default implementation.
- AreCloseOverrideF
Optional override for single-precision floating-point equality comparison. If set, this delegate will be used by
AreClose(float, float)instead of the default implementation.
- DoubleEpsilon
Represents the smallest positive double value such that 1.0 + DoubleEpsilon is not equal to 1.0.
- FloatEpsilon
Represents the smallest positive value that can be added to 1.0f to produce a distinct value of type float.
- MaxIntAsDouble
Represents the maximum value of a 32-bit signed integer as a double-precision floating-point number.
- MaxIntAsFloat
Represents the maximum value of a 32-bit signed integer as a floating-point number.
- MinIntAsDouble
Represents the minimum value of a 32-bit signed integer as a double-precision floating-point number.
- MinIntAsFloat
Represents the minimum value of a 32-bit signed integer as a floating-point number.
Methods
- ApplyMinMax(double, double?, double?)
Applies minimum and maximum to the value if limitations are not
null.
- ApplyMinMax(int, int?, int?)
Applies minimum and maximum to the value if limitations are not
null.
- ApplyMinMax(float, float?, float?)
Applies minimum and maximum to the value if limitations are not
null.
- AreClose(double, double)
Determines whether two double values are equal using a layered comparison strategy. This method combines magnitude-scaled epsilon comparison, bitwise proximity (with default step threshold), and relative/absolute tolerance checks. It is suitable for robust floating-point validation across domains such as diagnostics, serialization, and numerical correctness.
- AreClose(float, float)
Determines whether two float values are equal using a layered comparison strategy. This method combines magnitude-scaled epsilon comparison, bitwise proximity (with default step threshold), and relative/absolute tolerance checks. It is suitable for robust floating-point validation across domains such as diagnostics, serialization, and numerical correctness.
- AreCloseBitwise(double, double)
Determines whether two double-precision floating-point numbers are equal within a small bitwise difference.
- AreCloseBitwise(float, float)
Returns true if the two single-precision floats are close enough to be considered equal, based on bitwise proximity. Handles sign differences explicitly.
- AreCloseBitwiseEx(double, double, int)
Determines whether two double values are equal within a specified number of representable steps. This comparison is based on the binary distance between the two values in IEEE 754 format, making it suitable for bitwise proximity checks, serialization validation, and low-level diagnostics.
- AreCloseRect(RectD, RectD)
Determines whether two RectD instances are approximately equal.
- AreCloseScaled(double, double)
Returns true if the two doubles are close enough to be considered equal. Handles infinities and near-zero values.
- AreCloseScaled(float, float)
Returns true if the two single-precision floats are close enough to be considered equal. Handles infinities and near-zero values using scaled epsilon.
- AreCloseSize(SizeD, SizeD)
Determines whether two SizeD instances are approximately equal by comparing their width and height values.
- AreCloseWithTolerance(double, double)
Determines whether two double-precision floating-point numbers are approximately equal within a specified relative and absolute tolerance.
- AreCloseWithTolerance(float, float)
Returns true if the two single-precision floats are close enough to be considered equal, using both relative and absolute tolerance.
- AreCloseWithToleranceEx(double, double, double, double)
Determines whether two double-precision floating-point numbers are approximately equal within a specified relative and absolute tolerance.
- AreCloseWithToleranceEx(float, float, float, float)
Returns true if the two single-precision floats are close enough to be considered equal, using both relative and absolute tolerance thresholds.
- Ceiling(double)
Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.
- Ceiling(float)
Returns the smallest integral value greater than or equal to the specified single-precision float. Behaves like Ceiling(double) including handling of NaN and infinities.
- CeilingAsInt(double)
Returns the smallest integer greater than or equal to the specified double-precision floating-point number.
- Clamp(int, int, int)
Returns int value clamped to the inclusive range of min and max.
- ClampCoord(float, float, float)
Returns coordinate value clamped to the inclusive range of min and max.
- ClampD(double, double, double)
Returns double value clamped to the inclusive range of min and max.
- ClampToZero(double)
Clamps the specified value to zero if it is less than zero.
- ClampToZero(float)
Clamps the specified value to zero if it is less than zero.
- CombineHashCodes(int, int)
Combines two integer hash codes into a single value using high-entropy bit mixing.
- GetDistance(float, float, float, float)
Calculates distance between two points.
- GreaterThanAndNotClose(double, double)
Determines whether the first value is greater than the second value and not approximately equal to it.
- GreaterThanAndNotClose(float, float)
Determines whether the first value is greater than the second value and not approximately equal to it.
- GreaterThanOrClose(double, double)
Determines whether the first double-precision floating-point value is greater than or approximately equal to the second value.
- GreaterThanOrClose(float, float)
Determines whether the first floating-point value is greater than or approximately equal to the second value.
- IncrementIfOdd(int)
Increments the given number by 1 if it is odd; otherwise, returns the number unchanged.
- IsEvenInteger(double)
Returns true when the provided double represents an integer with no fractional part and that integer is even. NaN and Infinity return false. Uses the 2^53 shortcut: all representable double integers with absolute value >= 2^53 have spacing >= 2, so any such representable integer is even.
- IsEvenInteger(float)
Determines whether a single-precision floating-point value represents an even integer.
- IsInfinityOrNan(double)
Determines whether the specified
valueis either infinite (positive or negative) or not a number (NaN).
- IsInfinityOrNan(float)
Determines whether the specified
valueis either infinite (positive or negative) or not a number (NaN).
- IsInfinityOrNanOrMax(double)
Determines whether the specified double-precision floating-point value is NaN, infinite, or equal to the maximum representable value.
- IsInfinityOrNanOrMax(float)
Determines whether the specified floating-point value is NaN, infinite, or equal to the maximum representable value.
- IsInteger(double)
Determines whether the specified double-precision floating-point number represents an integer value.
- IsInteger(float)
Determines whether the specified floating-point value represents an integer.
- IsNaN(double)
Determines whether the specified double-precision floating-point value is NaN (Not a Number). This method uses bit-level inspection for performance-critical scenarios.
- IsNaN(float)
Determines whether the specified single-precision floating-point value is NaN (Not a Number). This method uses bit-level inspection.
- IsNegative(float)
Determines whether the specified floating-point number is negative.
- IsOne(double)
Determines whether the specified value is equal to 1.0 within a small tolerance.
- IsOne(float)
Determines whether the specified value is equal to 1.0 within a small tolerance.
- IsZero(double)
Determines whether the specified double-precision floating-point value is equal to zero.
- IsZero(float)
Determines whether the specified floating-point value is equal to zero.
- LessThanAndNotClose(double, double)
Determines whether the first value is less than the second value and not approximately equal to it.
- LessThanAndNotClose(float, float)
Determines whether the first value is less than the second value and not approximately equal to it.
- LessThanDefault(TypeCode, object?)
Compares
valuewith the default value of the specified type.
- LessThanOrClose(double, double)
Determines whether the first double-precision floating-point value is less than or approximately equal to the second value.
- LessThanOrClose(float, float)
Determines whether the first floating-point value is less than or approximately equal to the second value.
- MapRanges(double, double, double, double, double)
Maps value from range specified by (
fromLow,fromHigh) to range specified by (toLow,toHigh).
- Max(params double[])
Returns the larger of the specified numbers.
- Max(params int[])
Returns the larger of the specified numbers.
- Max(params int?[])
Returns the larger of the specified numbers.
- Max(object?, object?)
Returns the larger of two objects. Objects must support IComparable interface.
- Max(params float[])
Returns the larger of the specified numbers.
- Min(params double[])
Returns the smaller of the specified numbers.
- Min(params int[])
Returns the smaller of the specified numbers.
- Min(object?, object?)
Returns the smaller of two objects. Objects must support IComparable interface.
- Min(params float[])
Returns the smaller of the specified numbers.
- PercentOf(double, double)
Gets percentage of
value.
- PercentOf(float, float)
Gets percentage of
value.
- RoundToInt(double)
Rounds a double-precision floating-point number to the nearest integer, rounding away from zero for halfway cases.
- RoundUpAndIncrementIfOdd(float)
Rounds up the given coordinate value to the nearest integer and increments it by 1 if the result is odd.
- SafeCeilCoordToInt(float)
Converts a floating-point value to the smallest integer greater than or equal to the value.
- SafeCompareTo(object, object)
Same as CompareTo(object) but additionally allows to compare different integer types without exceptions. Objects must support IComparable interface.
- SequentialValuesHash<T>(IEnumerable<T>)
Computes a sequence-sensitive hash code for a collection of values using high-entropy mixing.
- ToDegrees(double)
Converts an angle from radians to degrees.
- ToRadians(double)
Converts an angle from degrees to radians.
- ToRadians(float)
Converts an angle from degrees to radians.
- ValueInRange(object?, object?, object?)
Checks whether
valueis in range specified withminValueandmaxValueparameters.
- ValueOrMin(long?, long)
Returns
minifvalueis null or less thanmin; otherwise returnsvalue.