Commit 0dcd7f35 authored by Edward Fauchon-Jones's avatar Edward Fauchon-Jones
Browse files

Add isclose floating point comparison function

parent d0bb2777
......@@ -183,6 +183,39 @@ class InterfieldSpec(Spec):
return
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
"""A Function for testing approximate equality
For backward compitibilty when not running with Python >= 3.5.
**References**
- https://www.python.org/dev/peps/pep-0485/
- https://docs.python.org/3.5/library/math.html#math.isclose
Parameters
----------
a, b: float
Values to comapre.
rel_tol: float
The relative tolerance - it is the maximum allowed difference between a
and b, relative to the larger absolute value of a or b. For example, to
set a tolerance of 5%, pass rel_tol=0.05. The default tolerance is
1e-09, which assures that the two values are the same within about 9
decimal digits. rel_tol must be greater than zero.
abs_tol: float
The minimum absolute tolerance - useful for comparisons near zero.
abs_tol must be at least zero.
Returns
-------
isclose: bool
Return True if the values a and b are close to each other and False
otherwise.
"""
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
# General Fields
class Type(Spec):
"""Specification for the `type` field"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment