| by Arround The Web | No comments

## Python Math Isclose() Method

In Python, different libraries and modules are utilized for applications such as computer vision, ML/machine learning, and data analysis. The “math” module in Python can be utilized to perform/execute various mathematical operations using its various methods. This module provides a method named “math.isclose()” for determining the closeness of input values.

This guide will present detailed information about the “math.isclose()” method using different examples and via the below content:

## What is “math.isclose()” Method in Python?

The “math.isclose()” method of the “math” module is used to determine whether the input numbers are close/near to each other. This method determines the closeness by utilizing relative or absolute tolerance.

Syntax

math.isclose(x, y, rel_tol, abs_tol)

In the above syntax:

• The “x” parameter specified the “1st” value to verify/check for closeness.
• The “y” parameter represents the “2nd” value to verify/check for closeness.
• The “rel_tol” parameter indicates the relative tolerance that is the max authorized difference between values “x” and “y”. The default value is set to “1e-09”.
• The “abs_tol” parameter shows the minimum absolute tolerance that is used to compare the values near “0”. The default value is set to “0”.

Return Value

The “math.isclose()” method is used to retrieve the Boolean value “True” if the value is close. Otherwise, the “False” is retrieved if the value is not close.

Example 1: Determining the Closeness of the Values Using “math.isclose()” Method

The “math.isclose()” method takes the two values as an argument and retrieves the Boolean value. The Boolean value specifies whether the input numbers are near/close to each other. Let’s perform this example for better understanding:

import math
print(math.isclose(4.422, 4.456))
print(math.isclose(4.422, 4.422))
print(math.isclose(4.422, 4.242))
print(math.isclose(4.422, 4.422000004))

Here is the output of the above-provided code after the execution: Example 2: Determining the Closeness of the Values Using Absolute Tolerance Parameter of “math.isclose()” Method

The “abs_tol” parameter value is passed to the “math.isclose()” method to determine the closeness of the values by comparing values near “0”. Take the below code of “math.isclose()” method as an example:

import math
print(math.isclose(4.422, 4.456, abs_tol = 0.4))
print(math.isclose(4.422, 4.922, abs_tol = 0.4))
print(math.isclose(4.422, 3.942, abs_tol = 0.4))
print(math.isclose(4.422, 4.123, abs_tol = 0.4))

This code retrieves the below-provided output: Example 3: Determining the Closeness of the Values Using Relative Tolerance Parameter of “math.isclose()” Method

The “rel_tol” parameter value is used to specify the maximum allowed difference between the input values. This parameter value is passed to the “math.isclose()” method to check the closeness by retrieving the Boolean value using the relative tolerance. Let’s understand this via the code below:

import math
print(math.isclose(4.4, 3.4, rel_tol = 0.2))
print(math.isclose(4.4, 3.6, rel_tol = 0.2))
print(math.isclose(4.4, 4.4, rel_tol = 0.2))
print(math.isclose(4.4, 5.6, rel_tol = 0.2))

Output ## Conclusion

In Python, the “math.isclose()” method of the “math” module is used to determine whether the input numbers are near/close to each other. We can also determine the closeness by passing the absolute or relative tolerance parameter value to the “math.isclose()” method. This guide presented detailed information on Python’s “math.isclose()” method.

Source: linuxhint.com