طريقة Python cmath
مثال
قارن التقارب بين قيمتين مركبتين:
#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()
مما إذا كانت قيمتان مركبتان قريبتان أم لا. ترجع هذه الطريقة قيمة منطقية: 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 |
---|---|
نسخة بايثون: | 3.5 |
مزيد من الأمثلة
مثال
قارن التقارب بين قيمتين معقدتين حيث يتم تحديد التسامح المطلق:
#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))