Arithmetic Functions: Checking For Closeness
In this lesson, we’ll take a look at a function in the
math module called
isclose() to help you decide when two floats are close. Shouldn’t they have called it
areclose(), then? See what happens in the Python console if you type
0.2 + 0.1, whether that’s equal to
isclose() takes two required positional arguments. These are going to be numbers
b. And then it takes two optional arguments,
rel_tol (relative tolerance) with a default value of 1 times 10 to the -9, and then a second one called
abs_tol (absolute tolerance) with a default value of
isclose() is going to return a Boolean depending whether the following comparison is either
isclose() is going to compute the absolute difference between
b, so regardless of whether the difference is negative or positive, we want to know the magnitude of the difference and whether that is less than or equal to the maximum of a measure of the relative difference between the two numbers and the absolute difference of the numbers.
And so the default behavior of
isclose() is to check whether the absolute difference of the two numbers is relatively small. But if the numbers that you’re comparing are small, then that’s where the absolute tolerance will be important.
Let’s define some numbers here to work with. Call the first one
x, and notice that I’m using underscore character (
_), which makes it more readable when you’re inputting large numbers. And then another number, we’ll call it
y. This’ll be very similar.
So, you get
True, and that sort of makes sense. Relatively speaking, these numbers are pretty close. Let’s see this by computing the relative difference of the two numbers. This is the difference, and here I’m using
y first just to make sure that I get a positive, and then we’re going to divide that by the maximum of the two numbers.
And so if we check the computation that’s being done by
isclose(), the difference divided by the maximum of
b, and that is 2.82 times 10 to the -5, and that’s bigger than the relative tolerance used, which is 1 times 10 to the -9.
So the default behavior of the
isclose() function is to test whether two numbers are close relative to their size or to use a relative tolerance. So, in this case, relatively speaking, the numbers
b, they’re not close. Now, if you were actually more concerned about the difference—not necessarily relative to their magnitude, but just whether in terms of the decimal places, the overall absolute difference, if they were close—then that’s where you would use the absolute tolerance parameter.
then in this case we get
True. Now, if you recall the difference between them,
b - a, is 0.000999 and so on. And if we compare these numbers, we see that the difference should be
0.001, and so this difference here, instead of being
0.001, has to do with how rounding or truncation occurs by storing numbers as floating-point numbers using binary digits. So, in other words, we’re getting here a rounding error by the fact that these numbers can’t be stored exactly using binary representation. In any case, the point is, even if we had exactly the difference being
0.001, then these would still be considered close if we add this absolute tolerance because that is the exact difference between the two numbers.
06:00 So if you’re working with numbers and you want to check whether they’re close and you’re really just concerned about whether they’re close relative to their magnitude, you really don’t have to worry so much about putting in a value for the absolute tolerance. But if you’re also concerned about whether they’re close absolutely—meaning that once you take the difference, you want to know whether they’re equal up to a certain number of digits—then you do want to add some non-zero value to the absolute tolerance.
Become a Member to join the conversation.