Checking the equality of two float arrays is not straightforward, because two floats may be very close without being equal. In NumPy, it is possible to check for equality with allclose. This function checks for the equality of two arrays up to a given precision:
data = random.rand(2)*1e-3 small_error = random.rand(2)*1e-16 data == data + small_error # False allclose(data, data + small_error, rtol=1.e-5, atol=1.e-8) # True
The tolerance is given in terms of a relative tolerance bound, rtol, and an absolute error bound, atol. The command allclose is a short form of:Â
(abs(A-B) < atol+rtol*abs(B)).all()
Note that allclose can be also applied to scalars:
data = 1e-3 error = 1e-16 data == data + error # False allclose(data, data + error, rtol=1.e-5, atol=1.e-8) #True