📜  Python – cmath.isclose()函数

📅  最后修改于: 2022-05-13 01:55:06.646000             🧑  作者: Mango

Python – cmath.isclose()函数

cMath 模块包含许多用于复数数学运算的函数。 cmath.isclose()函数用于检查两个复数值是否接近。此函数中传递的值可以是int、float复数

下面的例子说明了上述函数的使用:

示例 #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