{"id":25447,"date":"2021-11-10T14:59:49","date_gmt":"2021-11-10T09:29:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=25447"},"modified":"2021-11-14T15:02:47","modified_gmt":"2021-11-14T09:32:47","slug":"python-cmath-isclose-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-cmath-isclose-method-with-examples\/","title":{"rendered":"Python cmath.isclose() Method with Examples"},"content":{"rendered":"
cmath.isclose() Method in Python:<\/strong><\/p>\n 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.<\/p>\n To determine whether the values are close, this method employs either a relative tolerance or an absolute tolerance.<\/p>\n It compares the values using the following formula:<\/p>\n abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n a:<\/strong> This is Required. The first value used to determine closeness<\/p>\n b:<\/strong> This is Required. The second value used to determine closeness<\/p>\n rel_tol:<\/strong> 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.<\/p>\n abs_tol:<\/strong> This is Optional. The absolute minimum tolerance. It is used to compare values close to zero. The value must be greater than zero.<\/p>\n Return Value:<\/strong><\/p>\n Returns a true or false value. If the values are close, True; otherwise, False.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n With giving absolute tolerance<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 …<\/p>\ncmath.isclose(a, b, rel_tol= value, abs_tol= value)<\/pre>\n
Given first Complex Number = 3+4j\r\nGiven second Complex Number = 3+4j<\/pre>\n
Checking if the given two complex values are close or not = True<\/pre>\n
Given first Complex Number = 5+2j\r\nGiven second Complex Number = 5+26j<\/pre>\n
Checking if the given two complex values are close or not = False<\/pre>\n
cmath.isclose() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import cmath module(for complex number operations) using the import keyword.\r\nimport cmath\r\n# Give the complex number as static input and store it in a variable.\r\ncomplexnumb1 = 3+4j\r\n# Give the second complex number as static input and store it in another variable.\r\ncomplexnumb2 = 3+4j\r\n# Pass the given two complex numbers as the arguments to the cmath.isclose()\r\n# method that determines whether or not given two complex values are close.\r\n# Store it in another variable.\r\nrslt = cmath.isclose(complexnumb1, complexnumb2)\r\n# Print the above result\r\nprint(\"Checking if the given two complex values are close or not =\", rslt)\r\n<\/pre>\n
Checking if the given two complex values are close or not = True<\/pre>\n
\n
# Import cmath module(for complex number operations) using the import keyword.\r\nimport cmath\r\n# Give the complex number as static input and store it in a variable.\r\ncomplexnumb1 = 5+2j\r\n# Give the second complex number as static input and store it in another variable.\r\ncomplexnumb2 = 5+2j\r\n# Pass the given two complex numbers and absolute tolerance as some random number\r\n# as the arguments to the cmath.isclose() method that determines whether or\r\n# not given two complex values are close.\r\n# Store it in another variable.\r\nrslt = cmath.isclose(complexnumb1, complexnumb2, abs_tol=0.005)\r\n# Print the above result\r\nprint(\"Checking if the given two complex values are close or not =\", rslt)\r\n<\/pre>\n
Checking if the given two complex values are close or not = True<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import cmath module(for complex number operations) using the import keyword.\r\nimport cmath\r\n# Give the real part and imaginary part of the first complex number as user input\r\n# using map(), int(), split().\r\n# Store it in two variables.\r\nrealnumb1, imaginarynumb1 = map(int, input(\r\n 'Enter real part and complex part of the complex number = ').split())\r\n# Using a complex() function convert those two variables into a complex number.\r\ncomplexnumb1 = complex(realnumb1, imaginarynumb1)\r\n# Give the real part and imaginary part of the second complex number as user input\r\n# using map(), int(), split().\r\n# Store it in two variables.\r\nrealnumb2, imaginarynumb2 = map(int, input(\r\n 'Enter real part and complex part of the complex number = ').split())\r\n# Using a complex() function convert those two variables into a complex number.\r\ncomplexnumb2 = complex(realnumb2, imaginarynumb2)\r\n\r\n# Pass the given two complex numbers as the arguments to the cmath.isclose()\r\n# method that determines whether or not given two complex values are close.\r\n# Store it in another variable.\r\nrslt = cmath.isclose(complexnumb1, complexnumb2)\r\n# Print the above result\r\nprint(\"Checking if the given two complex values are close or not =\", rslt)\r\n\r\n<\/pre>\n
Enter real part and complex part of the complex number = 5 2\r\nEnter real part and complex part of the complex number = 3 2\r\nChecking if the given two complex values are close or not = False<\/pre>\n","protected":false},"excerpt":{"rendered":"