pub fn approxEqAbs(comptime T: type, x: T, y: T, tolerance: T) bool T: typex: Ty: Ttolerance: T77
78
79
80
81
82
83
84
85
86
87
88
89pub fn approxEqAbs(comptime T: type, x: T, y: T, tolerance: T) bool {
comptime assert(@typeInfo(T) == .float or @typeInfo(T) == .comptime_float);
assert(tolerance >= 0);
// Fast path for equal values (and signed zeros and infinites).
if (x == y)
return true;
if (isNan(x) or isNan(y))
return false;
return @abs(x - y) <= tolerance;
}
Performs an approximate comparison of two floating point values
xandy. Returns true if the absolute difference between them is less or equal than the specified tolerance.The
toleranceparameter is the absolute tolerance used when determining if the two numbers are close enough; a good value for this parameter is a small multiple offloatEps(T).Note that this function is recommended for comparing small numbers around zero; using
approxEqRelis suggested otherwise.NaN values are never considered equal to any value.