Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Arithmetic Functions: Checking For Closeness

00:00 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 0.3.

00:20 You’re going to get False. Now, this isn’t a bug in Python. It has to do with the ability of computers to store real numbers using binary representation.

00:30 A better approach to checking whether two numbers are equal is to check whether they’re close. The math module provides a function called isclose() to do exactly this.

00:41 The function isclose() takes two required positional arguments. These are going to be numbers a and 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 0.0.

01:01 isclose() is going to return a Boolean depending whether the following comparison is either True or False. isclose() is going to compute the absolute difference between a and 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.

01:28 Now, because the absolute tolerance is set to 0.0, the default value that will be checked is going to be this relative tolerance times the maximum of the magnitudes of a and b.

01:40 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.

01:54 Let’s take a look at a couple of examples just for you to see how this works.

02:00 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.

02:15 The difference is that the decimal part is going to be .410.

02:20 Now, are these numbers close? Well, let’s just see what isclose() returns.

02:27 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.

02:47 And they’re both positive, so I don’t need absolute values—just the maximum of x and y. The difference of the two numbers relative to their size is 2.8 times 10 to the -10.

03:01 And that is indeed less than the relative difference of the one used in the isclose() function, which is 1 times 10 to the 9.

03:13 Now let’s put in another number, we’ll call it a.

03:18 And then b is going to be 35.410. The only difference between the numbers x and y that we had before, if I bring these up,

03:28 is that the integer part is much larger. They have the same decimal parts, so .409 and .410, but here they’re much smaller. So, this was 3 million. Here, they’re both 35.

03:43 All right, now let’s check whether these are close.

03:49 And this makes sense, I guess, because they’re not very big numbers, so the difference between the two numbers, b - a, relative to their size is significant.

04:01 And so if we check the computation that’s being done by isclose(), the difference divided by the maximum of a and 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.

04:20 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 a and 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.

04:57 Now, if I test whether these are close and use, say, an absolute tolerance of 0.001,

05:08 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.

06:28 This wraps up our look into the arithmetic functions in the math module. In the next lesson, we’ll take a look at the power function.

Become a Member to join the conversation.