xxxxxxxxxx
import numpy as np
np.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)
xxxxxxxxxx
# Python compare floats
import math
0.1 + 0.2 == 0.3 # False
math.isclose(0.1 + 0.2, 0.3) # True
0.8 - 0.1 > 0.7 # True
num1 = 0.8 - 0.1
num2 = 0.7
not math.isclose(num1, num2) and num1 > num2 # False
xxxxxxxxxx
# credit to the Stack Overflow user in the source link
# method of the 'math' module
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)