Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test whether a value is within some range with some fuzziness at the edges to allow for floating point noise.
import numpy as np
def fuzzy_between(val, minval, maxval, fuzz=2, inclusive=True):
"""
Test whether a value is within some range with some fuzziness at the edges
to allow for floating point noise.
The fuzziness is implemented by expanding the range at each end `fuzz` steps
using the numpy.nextafter function. For example, with the inputs
minval = 1, maxval = 2, and fuzz = 2; the range would be expanded to
minval = 0.99999999999999978 and maxval = 2.0000000000000009 before doing
comparisons.
Parameters
----------
val : float
Value being tested.
minval : float
Lower bound of range. Must be lower than `maxval`.
maxval : float
Upper bound of range. Must be higher than `minval`.
fuzz : int, optional
Number of times to expand bounds using numpy.nextafter.
inclusive : bool, optional
Set whether endpoints are within the range.
Returns
-------
is_between : bool
True if `val` is between `minval` and `maxval`, False otherwise.
"""
# expand bounds
for _ in xrange(fuzz):
minval = np.nextafter(minval, minval - 1e6)
maxval = np.nextafter(maxval, maxval + 1e6)
if inclusive:
return minval <= val <= maxval
else:
return minval < val < maxval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment