Python – cmath.isclose()函数
cMath 模块包含许多用于复数数学运算的函数。 cmath.isclose()函数用于检查两个复数值是否接近。此函数中传递的值可以是int、float和复数。
Syntax: cmath.isclose(a, b, rel_tol = value, abs_tol = value)
Parameter:This method accepts the following parameters.
- a :This parameter is the first value to check for closeness.
- b :This parameter is the second value to check for closeness.
- rel_tol = value :This parameter is the maximum allowed difference between value a and b.
- abs_tol = value : This parameter is the minimum absolute tolerance
Returns:This method returns a Boolean value.
下面的例子说明了上述函数的使用:
示例 #1:
Python3
# Python code to implement
# the isclose()function
# importing "cmath"
# for mathematical operations
import cmath
# using cmath.isclose() method
val = cmath.isclose(1 + 2j, 1 + 2j)
print(val)
val1 = cmath.isclose(1 + 2.2j, 1 + 2j)
print(val1)
Python3
# Python code to implement
# the isclose()function
# importing "cmath"
# for mathematical operations
import cmath
# using cmath.isclose() method
val = cmath.isclose(1 + 2j, 1 + 2j, abs_tol = 0.5)
print(val)
val1 = cmath.isclose(1 + 2.2j, 1 + 2j, abs_tol = 0.5)
print(val1)
输出:
True
False
示例 2:
Python3
# Python code to implement
# the isclose()function
# importing "cmath"
# for mathematical operations
import cmath
# using cmath.isclose() method
val = cmath.isclose(1 + 2j, 1 + 2j, abs_tol = 0.5)
print(val)
val1 = cmath.isclose(1 + 2.2j, 1 + 2j, abs_tol = 0.5)
print(val1)
输出:
True
True