Python matematik.isclose() Yöntem
Örnek
İki değerin birbirine yakın olup olmadığını kontrol edin:
#Import math Library
import math
#compare the closeness of two values
print(math.isclose(1.233, 1.4566))
print(math.isclose(1.233, 1.233))
print(math.isclose(1.233, 1.24))
print(math.isclose(1.233, 1.233000001))
Tanım ve Kullanım
Yöntem math.isclose()
, iki değerin birbirine yakın olup olmadığını kontrol eder. Değerler yakınsa True, değilse False döndürür.
Bu yöntem, değerlerin yakın olup olmadığını görmek için göreli veya mutlak bir tolerans kullanır.
İpucu: Değerleri karşılaştırmak için şu formülü kullanır: abs(ab) <= max(rel_tol * max(abs(a), abs(b))), abs_tol)
Sözdizimi
math.isclose(a, b, rel_tol, abs_tol)
Parametre Değerleri
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 |
Teknik detaylar
Geri dönüş değeri: | Bir bool değer. True değerler yakınsa, aksi takdirdeFalse |
---|---|
Python Sürümü: | 3.5 |
Daha fazla örnek
Örnek
Mutlak tolerans kullanın:
#Import math Library
import math
#compare the closeness of two
values
print(math.isclose(8.005, 8.450, abs_tol = 0.4))
print(math.isclose(8.005, 8.450, abs_tol = 0.5))