Table of Contents

Method AreCloseWithToleranceEx

Namespace
Alternet.UI
Assembly
Alternet.UI.Common.dll

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

a double

The first double-precision floating-point number to compare.

b double

The second double-precision floating-point number to compare.

relTol double

The 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.

absTol double

The 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 a and b is 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

a float

First value to compare.

b float

Second value to compare.

relTol float

Relative tolerance, scaled by the magnitude of the inputs. Default is 1e-6f.

absTol float

Absolute tolerance, used for near-zero comparisons. Default is 1e-8f.

Returns

bool

true if 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.