cmath.isclose() Method in Python:
The cmath.isclose() method determines whether or not two complex values are close. This method gives the following Boolean value: If the values are close, True; otherwise, False.
To determine whether the values are close, this method employs either a relative tolerance or an absolute tolerance.
It compares the values using the following formula:
abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
Syntax:
cmath.isclose(a, b, rel_tol= value, abs_tol= value)
Parameters
a: This is Required. The first value used to determine closeness
b: This is Required. The second value used to determine closeness
rel_tol: This is Optional. It is relative tolerance. It is the greatest (maximum) possible difference between values a and b. 1e-09 is the default value.
abs_tol: This is Optional. The absolute minimum tolerance. It is used to compare values close to zero. The value must be greater than zero.
Return Value:
Returns a true or false value. If the values are close, True; otherwise, False.
Examples:
Example1:
Input:
Given first Complex Number = 3+4j Given second Complex Number = 3+4j
Output:
Checking if the given two complex values are close or not = True
Example2:
Input:
Given first Complex Number = 5+2j Given second Complex Number = 5+26j
Output:
Checking if the given two complex values are close or not = False
cmath.isclose() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import cmath module(for complex number operations) using the import keyword.
- Give the first complex number as static input and store it in a variable.
- Give the second complex number as static input and store it in another variable.
- Pass the given two complex numbers as the arguments to the cmath.isclose() method that determines whether or not given two complex values is close.
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import cmath module(for complex number operations) using the import keyword. import cmath # Give the complex number as static input and store it in a variable. complexnumb1 = 3+4j # Give the second complex number as static input and store it in another variable. complexnumb2 = 3+4j # Pass the given two complex numbers as the arguments to the cmath.isclose() # method that determines whether or not given two complex values are close. # Store it in another variable. rslt = cmath.isclose(complexnumb1, complexnumb2) # Print the above result print("Checking if the given two complex values are close or not =", rslt)
Output:
Checking if the given two complex values are close or not = True
With giving absolute tolerance
Approach:
- Import cmath module(for complex number operations) using the import keyword.
- Give the first complex number as static input and store it in a variable.
- Give the second complex number as static input and store it in another variable.
- Pass the given two complex numbers and absolute tolerance as some random number as the arguments to the cmath.isclose() method that determines whether or not given two complex values is close.
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import cmath module(for complex number operations) using the import keyword. import cmath # Give the complex number as static input and store it in a variable. complexnumb1 = 5+2j # Give the second complex number as static input and store it in another variable. complexnumb2 = 5+2j # Pass the given two complex numbers and absolute tolerance as some random number # as the arguments to the cmath.isclose() method that determines whether or # not given two complex values are close. # Store it in another variable. rslt = cmath.isclose(complexnumb1, complexnumb2, abs_tol=0.005) # Print the above result print("Checking if the given two complex values are close or not =", rslt)
Output:
Checking if the given two complex values are close or not = True
Method #2: Using Built-in Functions (User Input)
Approach:
- Import cmath module(for complex number operations) using the import keyword.
- Give the real part and imaginary part of the first complex number as user input using map(), int(), split().
- Store it in two variables.
- Using a complex() function convert those two variables into a complex number and store them in a variable.
- Give the real part and imaginary part of the second complex number as user input using map(), int(), split().
- Store it in two variables.
- Using a complex() function convert those two variables into a complex number and store them in a variable.
- Pass the given two complex numbers as the arguments to the cmath.isclose() method that determines whether or not given two complex values is close.
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import cmath module(for complex number operations) using the import keyword. import cmath # Give the real part and imaginary part of the first complex number as user input # using map(), int(), split(). # Store it in two variables. realnumb1, imaginarynumb1 = map(int, input( 'Enter real part and complex part of the complex number = ').split()) # Using a complex() function convert those two variables into a complex number. complexnumb1 = complex(realnumb1, imaginarynumb1) # Give the real part and imaginary part of the second complex number as user input # using map(), int(), split(). # Store it in two variables. realnumb2, imaginarynumb2 = map(int, input( 'Enter real part and complex part of the complex number = ').split()) # Using a complex() function convert those two variables into a complex number. complexnumb2 = complex(realnumb2, imaginarynumb2) # Pass the given two complex numbers as the arguments to the cmath.isclose() # method that determines whether or not given two complex values are close. # Store it in another variable. rslt = cmath.isclose(complexnumb1, complexnumb2) # Print the above result print("Checking if the given two complex values are close or not =", rslt)
Output:
Enter real part and complex part of the complex number = 5 2 Enter real part and complex part of the complex number = 3 2 Checking if the given two complex values are close or not = False