Add configurable tolerance to is_quantity_close
@@ -388,7 +388,7 @@ _VALID_UNIT_FUNCTIONS = {method: getattr(unit, method) for method in dir(unit)
if callable(getattr(unit, method)) and type(getattr(unit, method)) is not type}
-def is_quantity_close(quantity1, quantity2):
+def is_quantity_close(quantity1, quantity2, rtol=1e-10, atol=0.0):
"""Check if the quantities are equal up to floating-point precision errors.
Parameters
@@ -397,6 +397,10 @@ def is_quantity_close(quantity1, quantity2):
The first quantity to compare.
quantity2 : simtk.unit.Quantity
The second quantity to compare.
+ rtol : float, optional
+ Relative tolerance (default is 1e-10).
+ atol : float, optional
+ Absolute tolerance (default is 0.0).
Returns
-------
@@ -417,9 +421,9 @@ def is_quantity_close(quantity1, quantity2):
# np.isclose is not symmetric, so we make it so.
if abs(value2) >= abs(value1):
- return np.isclose(value1, value2, rtol=1e-10, atol=0.0)
+ return np.isclose(value1, value2, rtol=rtol, atol=atol)
else:
- return np.isclose(value2, value1, rtol=1e-10, atol=0.0)
+ return np.isclose(value2, value1, rtol=rtol, atol=atol)
def quantity_from_string(expression):
