Class NumericUtils
- Namespace
- SignalSharp.Utilities
- Assembly
- SignalSharp.dll
Provides utility methods for common numerical operations with generic number support.
public static class NumericUtils
- Inheritance
-
NumericUtils
- Inherited Members
Remarks
This utility class implements type-safe numerical operations using the generic math capabilities provided by INumber<TSelf>. It offers consistent ways to handle various numeric comparisons, equality checks, and rounding operations across different numeric types.
Methods
AreApproximatelyEqualRelative<T>(T, T)
Checks if two floating-point values are approximately equal using default relative and absolute tolerances for the type.
public static bool AreApproximatelyEqualRelative<T>(T a, T b) where T : INumber<T>, IFloatingPointIeee754<T>
Parameters
a
TThe first value to compare.
b
TThe second value to compare.
Returns
- bool
True if the values are close enough based on default tolerances.
Type Parameters
T
A numeric type that implements IFloatingPointIeee754<TSelf>.
Remarks
Uses default tolerances obtained from GetDefaultRelativeEpsilon<T>() and GetDefaultEpsilon<T>(). This is often the preferred method for comparing floating-point numbers for equality. See remarks on AreApproximatelyEqualRelative<T>(T, T, T, T).
AreApproximatelyEqualRelative<T>(T, T, T, T)
public static bool AreApproximatelyEqualRelative<T>(T a, T b, T relativeTolerance, T absoluteTolerance) where T : INumber<T>, IFloatingPoint<T>
Parameters
a
Tb
TrelativeTolerance
TabsoluteTolerance
T
Returns
Type Parameters
T
AreApproximatelyEqual<T>(T, T)
Checks if two numeric values are approximately equal using the default absolute tolerance (epsilon) for the type.
public static bool AreApproximatelyEqual<T>(T a, T b) where T : INumber<T>
Parameters
a
TThe first value to compare.
b
TThe second value to compare.
Returns
- bool
True if the absolute difference between
a
andb
is less than or equal to the default absolute epsilon.
Type Parameters
T
A numeric type that implements INumber<TSelf>.
Remarks
Uses a type-specific default absolute epsilon value obtained via GetDefaultEpsilon<T>(). See the remarks on AreApproximatelyEqual<T>(T, T, T) regarding the limitations of absolute tolerance. Consider using AreApproximatelyEqualRelative<T>(T, T) for potentially more robust comparisons.
AreApproximatelyEqual<T>(T, T, T)
public static bool AreApproximatelyEqual<T>(T a, T b, T absoluteEpsilon) where T : INumber<T>
Parameters
a
Tb
TabsoluteEpsilon
T
Returns
Type Parameters
T
GetDefaultEpsilon<T>()
Gets the appropriate default epsilon value for the numeric type.
public static T GetDefaultEpsilon<T>() where T : INumber<T>
Returns
- T
A type-appropriate default epsilon value.
Type Parameters
T
A numeric type that implements INumber<TSelf>.
Remarks
Provides type-specific epsilon values optimized for each numeric type's precision characteristics. Falls back to a reasonable default for other numeric types.
GetDefaultRelativeEpsilon<T>()
Gets the appropriate default relative epsilon value for floating-point types.
public static T GetDefaultRelativeEpsilon<T>() where T : INumber<T>, IFloatingPointIeee754<T>
Returns
- T
A type-appropriate default relative epsilon value.
Type Parameters
T
A numeric type that implements IFloatingPointIeee754<TSelf>.
Remarks
Provides type-specific relative epsilon values for use in relative comparisons. Returns T.Zero for non-floating point types or if relative tolerance is not applicable.
GetStrictEpsilon<T>()
Gets the appropriate strict epsilon value for the numeric type.
public static T GetStrictEpsilon<T>() where T : INumber<T>
Returns
- T
A type-appropriate strict epsilon value for high-precision operations.
Type Parameters
T
A numeric type that implements INumber<TSelf>.
Remarks
Provides stricter tolerance values for operations requiring higher precision. Use this for numerically sensitive calculations like model fitting or matrix operations.
GetVarianceEpsilon<T>()
Gets the appropriate variance epsilon value for the numeric type.
public static T GetVarianceEpsilon<T>() where T : INumber<T>
Returns
- T
A type-appropriate epsilon value for variance calculations.
Type Parameters
T
A numeric type that implements INumber<TSelf>.
Remarks
Provides epsilon values specifically calibrated for variance calculations to prevent division by zero and logarithm of zero errors in statistical models.
IsEffectivelyInteger<T>(T)
Checks if a floating-point value is effectively an integer using the default epsilon for the type.
public static bool IsEffectivelyInteger<T>(T value) where T : INumber<T>, IFloatingPoint<T>
Parameters
value
TThe value to check.
Returns
- bool
True if the value is within the default epsilon of an integer.
Type Parameters
T
A numeric type that implements both INumber<TSelf> and IFloatingPoint<TSelf>.
Remarks
Uses a type-specific default epsilon value appropriate for the numeric type T.
IsEffectivelyInteger<T>(T, T)
Checks if a floating-point value is effectively an integer within the specified epsilon.
public static bool IsEffectivelyInteger<T>(T value, T epsilon) where T : INumber<T>, IFloatingPoint<T>
Parameters
value
TThe value to check.
epsilon
TThe maximum allowed difference from a whole number.
Returns
- bool
True if the value is within
epsilon
of an integer.
Type Parameters
T
A numeric type that implements both INumber<TSelf> and IFloatingPoint<TSelf>.
Remarks
This is useful for determining if a floating-point value should be treated as an integer, accounting for potential rounding errors in floating-point arithmetic.
IsEffectivelyZero<T>(T)
Checks if a value is effectively zero using the default absolute tolerance (epsilon) for the type.
public static bool IsEffectivelyZero<T>(T value) where T : INumber<T>
Parameters
value
TThe value to check.
Returns
- bool
True if the absolute value is less than or equal to the default absolute epsilon.
Type Parameters
T
A numeric type that implements INumber<TSelf>.
Remarks
Uses a type-specific default absolute epsilon value obtained via GetDefaultEpsilon<T>(). See remarks on IsEffectivelyZero<T>(T, T).
IsEffectivelyZero<T>(T, T)
public static bool IsEffectivelyZero<T>(T value, T absoluteEpsilon) where T : INumber<T>
Parameters
value
TabsoluteEpsilon
T
Returns
Type Parameters
T