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
AreApproximatelyEqual<T>(T, T)
Checks if two numeric values are approximately equal within the default 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 the default epsilon.
Type Parameters
T
A numeric type that implements INumber<TSelf>.
Remarks
Uses a type-specific default epsilon value appropriate for the numeric type T.
AreApproximatelyEqual<T>(T, T, T)
Checks if two numeric values are approximately equal within the specified epsilon.
public static bool AreApproximatelyEqual<T>(T a, T b, T epsilon) where T : INumber<T>
Parameters
a
TThe first value to compare.
b
TThe second value to compare.
epsilon
TThe maximum difference allowed while considering the values equal.
Returns
- bool
True if the absolute difference between
a
andb
is less thanepsilon
.
Type Parameters
T
A numeric type that implements INumber<TSelf>.
Remarks
This method accounts for floating-point imprecision by considering two values equal if their difference is smaller than the specified tolerance.
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.
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 within the default 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 the default epsilon.
Type Parameters
T
A numeric type that implements INumber<TSelf>.
Remarks
Uses a type-specific default epsilon value appropriate for the numeric type T.
IsEffectivelyZero<T>(T, T)
Checks if a value is effectively zero within the specified epsilon.
public static bool IsEffectivelyZero<T>(T value, T epsilon) where T : INumber<T>
Parameters
value
TThe value to check.
epsilon
TThe maximum absolute value to consider as effectively zero.
Returns
- bool
True if the absolute value is less than
epsilon
.
Type Parameters
T
A numeric type that implements INumber<TSelf>.
Remarks
This method is useful for handling values that should be zero but might have small non-zero values due to floating-point arithmetic imprecisions.