0

I have the following method in my python code that compares values between two objects to determine if they are equal:

def equals(self, vec, tol):
        return all(i < tol for i in [abs(a - b) for a, b in zip(self, vec)])

I want to give a default value to my tolerance variable, tol, such that it is the smallest possible value that is always greater than error that could occur from floating-point inaccuracies. What is this value?

3
  • 1
    Depending on the arithmetic operations, the error can become arbitrarily large. Commented Oct 7, 2018 at 1:14
  • True, so should the tolerance depend on the size of the values I'm comparing? Commented Oct 7, 2018 at 1:20
  • Right. One usual way is to multiply tolerance with max(abs(a), abs(b)) Commented Oct 7, 2018 at 1:24

2 Answers 2

1

The largest possible error is infinity, and NaN (Not a Number) is also possible. There is no general formula that is correct for tol. Determining what error could occur always requires knowledge of the values used and the operations performed.

Additionally, there are limited situations where “comparing for equality using a tolerance” is a proper technique. (Testing software is one of them.) Comparing for equality using a tolerance reduces the risk of deciding two numbers are unequal even though they would be equal if computed with exact mathematics, but it does so at the expense of falsely accepting as equal two numbers that would be unequal if computed with exact mathematics. Even deciding whether such a trade-off is acceptable is application-dependent, let alone deciding what the tolerance should be.

Sign up to request clarification or add additional context in comments.

2 Comments

I have come to understand what you said in your first paragraph by previous comments. However, I don't understand your described trade off. If two numbers differ by less than the default tolerance (which is the smallest possible floating point error) can a computer even reliably distinguish those two numbers? If so, couldn't a 0 tolerance be passed to get the regular expected behavior?
If two numbers x and y are close together and both round to the same floating-point number, then no direct computation of those numbers would be able to distinguish them. If one were attempting to distinguish them, one might compute x*−*y (by which I mean perform some computation whose result is mathematically x*−*y, not to compute an x and a y and then subtract them) and compare it to zero. Regarding your second question, a zero tolerance would cause the equals in the question to always return false, as it uses < tol. With <= tol, a zero tol would make it effectively ==.
0

I usually use something like this with numpy:

tol = max(np.finfo(float).eps, np.finfo(float).eps * abs(a - b))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.