Method AreCloseWithToleranceEx
AreCloseWithToleranceEx(double, double, double, double)
Determines whether two double-precision floating-point numbers are approximately equal within a specified relative and absolute tolerance.
public static bool AreCloseWithToleranceEx(double a, double b, double relTol = 1E-12, double absTol = 1E-15)
Parameters
adoubleThe first double-precision floating-point number to compare.
bdoubleThe second double-precision floating-point number to compare.
relToldoubleThe relative tolerance, which defines the allowable difference relative to the larger of the two values. Must be a non-negative value. Defaults to
1e-12.absToldoubleThe absolute tolerance, which defines the minimum allowable difference regardless of scale. Must be a non-negative value. Defaults to
1e-15.
Returns
- bool
true if the difference between
aandbis less than or equal to the greater of the relative or absolute tolerance; otherwise, false.
Remarks
This method is useful for comparing floating-point numbers where precision errors may occur due to the limitations of binary representation. The comparison accounts for both the scale of the numbers and a fixed minimum 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.
public static bool AreCloseWithToleranceEx(float a, float b, float relTol = 1E-06, float absTol = 1E-08)
Parameters
afloatFirst value to compare.
bfloatSecond value to compare.
relTolfloatRelative tolerance, scaled by the magnitude of the inputs. Default is 1e-6f.
absTolfloatAbsolute tolerance, used for near-zero comparisons. Default is 1e-8f.
Returns
- bool
trueif the values are close within the specified tolerances; otherwise,false.
Examples
AreCloseWithToleranceEx(1.0f, 1.000001f); // true
AreCloseWithToleranceEx(0.0f, 1e-8f); // false
AreCloseWithToleranceEx(1e-7f, 2e-7f, 1e-5f, 1e-7f); // true
AreCloseWithToleranceEx(float.NaN, float.NaN); // false
AreCloseWithToleranceEx(float.PositiveInfinity, float.PositiveInfinity); // true
Remarks
This method is robust across magnitudes and avoids false positives near zero. It is symmetric with AreCloseWithTolerance(double, double) and suitable for diagnostic-grade comparisons.