How to compare two float NumPy arrays using a tolerance in Python

1 Answer

0 votes
import numpy as np

a = np.array([12314.9872, 3.14, 12387.91371, 8876.579013])
b = np.array([12314.9872, 3.14, 12387.91372, 8876.579013])

'''
numpy.allclose in NumPy makes floating‑point comparison behave the way humans expect, not 
the way raw binary floats behave. It’s the standard tool for checking whether two arrays are 
"close enough" rather than exactly equal.
'''

print(np.allclose(a, b, atol=0.001))


"""
run:

True

"""

 



answered Sep 8 by avibootz
...