Python cmath.isclose()メソッド
例
2つの複素数値の近さを比較します。
#Import cmath Library
import cmath
#compare the closeness of two
complex values using relative tolerance
print(cmath.isclose(10+5j,
10+5j))
print(cmath.isclose(10+5j, 10.01+5j))
定義と使用法
このcmath.isclose()
メソッドは、2つの複素数値が近いかどうかをチェックします。このメソッドはブール値を返します。True
値が近い場合は、そうでない場合はFalse
。
この方法では、相対許容誤差または絶対許容誤差を使用して、値が近いかどうかを確認します。
ヒント:次の式を使用して値を比較します:
abs(ab)<= max(rel_tol * max(abs(a)、abs(b))、abs_tol)
構文
cmath.isclose(a, b, rel_tol = value, abs_tol = value)
パラメータ値
Parameter | Description |
---|---|
a | Required. The first value to check for closeness |
b | Required. The second value to check for closeness |
rel_tol = value | Optional. The relative tolerance. It is the maximum allowed difference between value a and b. Default value is 1e-09 |
abs_tol = value | Optional. The minimum absolute tolerance. It is used to compare values near 0. The value must be at least 0 |
技術的な詳細
戻り値: | bool 値。True 値が近い場合、そうでない場合。False |
---|---|
Pythonバージョン: | 3.5 |
その他の例
例
絶対許容誤差が定義されている2つの複素数値の近さを比較します。
#Import cmath Library
import cmath
#compare the closeness of two
complex values using absolute tolerance
print(cmath.isclose(10+5j, 10+5j,
abs_tol=0.005))
print(cmath.isclose(10+5j, 10.01+5j, abs_tol=0.005))