Python math library | isclose() method
In Python math module, math.isclose() method is used to determine whether two floating point numbers are close in value. For using this function you must import math module.
Syntax: isclose(a, b, rel_tol = 1e-09, abs_tol 0.0)
Parameters:
rel_tol: maximum difference for being considered “close”, relative to the magnitude of the input values
abs_tol: maximum difference for being considered “close”, regardless of the magnitude of the input values
->
rel_tolandabs_tolcan be changed by using keyword argument, or by simply providing directly as according to their positions in the parameter list.Return Value : Return True if a is close in value to b, and False otherwise.
Note: For the values to be considered close, the difference between them must be smaller than at least one of the tolerances.
Code #1:
# Importing Math module import math # printing whether two values are close or not print(math.isclose(2.005, 2.005)) print(math.isclose(2.005, 2.004)) print(math.isclose(2.006, 2.005)) |
Output:
True False False
Code #2:
# Importing Math module import math # printing whether two values are close or not print(math.isclose(2.005, 2.125, abs_tol = 0.25)) print(math.isclose(2.547, 2.0048, abs_tol = 0.5)) print(math.isclose(2.0214, 2.00214, abs_tol = 0.02)) |
Output:
True False True
You can change absolute tolerance, as in above case absolute tolerance is different in all three cases.
Reference: Python math library
Recommended Posts:
- Python math library | exp() method
- Python math library | isnan() method
- Python math library | expm1() method
- Python math library | isfinite() and remainder() method
- Python math library | gamma() function
- FuzzyWuzzy Python library
- Python | Holidays library
- Python | Schedule Library
- Python Faker Library
- Pytube | Python library to download youtube videos
- Python | math.tan() function
- Python | math.sin() function
- Python | math.cos() function
- Python | math.gcd() function
- Python math function | sqrt()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



