isApproximatelyEqual(to:absoluteTolerance:relativeTolerance:)
Test if self
and other
are approximately equal with specified tolerances.
func isApproximatelyEqual(to other: Self, absoluteTolerance: Magnitude, relativeTolerance: Magnitude = 0) -> Bool
Parameters
- other
The value to which
self
is compared.- absoluteTolerance
The absolute tolerance to use in the comparison.
This value should be non-negative and finite. This constraint on is only checked in debug builds, because a mathematically well-defined result exists for any tolerance, even one out of range.
- relativeTolerance
The relative tolerance to use in the comparison. Defaults to zero.
This value should be non-negative and less than or equal to 1. This constraint on is only checked in debug builds, because a mathematically well-defined result exists for any tolerance, even one out of range.
true
if self
and other
are equal, or if they are finite and either
(self - other).magnitude <= absoluteTolerance
or
(self - other).magnitude <= relativeTolerance * scale
where scale
is max(self.magnitude, other.magnitude)
.
Mathematical Properties:
isApproximatelyEqual(to:absoluteTolerance:relativeTolerance:)
is reflexive for non-exceptional values (such as NaN).isApproximatelyEqual(to:absoluteTolerance:relativeTolerance:)
is symmetric.isApproximatelyEqual(to:relativeTolerance:norm:)
is not transitive. Because of this, approximately equality is not an equivalence relation, even when restricted to non-exceptional values.This means that you must not use approximate equality to implement a conformance to Equatable, as it will violate the invariants of code written against that protocol.
For any point
a
, the set of values that compare approximately equal toa
is convex. (Under the assumption thatnorm
implements a valid norm, which cannot be checked by this function.)
See Also:
isApproximatelyEqual(to:[relativeTolerance:])