Skip to content

Instantly share code, notes, and snippets.

@iminurnamez
Created December 24, 2016 13:59
Show Gist options
  • Save iminurnamez/2973e5b3685576928f24e25e1d344cbb to your computer and use it in GitHub Desktop.
Save iminurnamez/2973e5b3685576928f24e25e1d344cbb to your computer and use it in GitHub Desktop.
class BoundedFloat(object):
def __init__(self, value, minimum, maximum):
assert minimum <= maximum
self._val = value
self.minimum = minimum
self.maximum = maximum
@property
def val(self):
return self._val
@val.setter
def val(self, other):
self._val = other
self.limit()
def __str__(self):
return str(self._val)
def limit(self):
if self._val < self.minimum:
self._val = self.minimum
elif self._val > self.maximum:
self._val = self.maximum
def __add__(self, other):
return self._val + other
def __radd__(self, other):
return self._val + other
def __iadd__(self, other):
self._val += other
self.limit()
return self
def __sub__(self, other):
return self._val - other
def __isub__(self, other):
self._val -= other
self.limit()
return self
def __rsub__(self, other):
return other - self._val
def __mul__(self, other):
return self._val * other
def __imul__(self, other):
self._val *= other
self.limit()
return self
def __rmul__(self, other):
return self._val * other
def __div__(self, other):
return self._val / other
def __idiv__(self, other):
self._val /= other
self.limit()
return self
def __floordiv__(self, other):
return self._val // other
def __ifloordiv__(self, other):
self._val //= other
self.limit()
return self
def __nonzero__(self):
return bool(self._val)
def __lt__(self, other):
return self._val < other
def __le__(self, other):
return self._val <= other
def __gt__(self, other):
return self._val > other
def __ge__(self, other):
return self._val >= other
def __eq__(self, other):
return self._val == other
def __ne__(self, other):
return self._val != other
def __neg__(self):
return -self._val
def __abs__(self):
return abs(self._val)
def __int__(self):
return int(self._val)
def __float__(self):
return float(self._val)
def test_addition(test_vals1, test_vals2):
val, minimum, maximum = test_vals1
b = BoundedFloat(val, minimum, maximum)
x = test_vals2[0]
assert b + x == val + x
b += x
assert b == val + x or (val + x > maximum and b == maximum)
b += maximum * 2
assert b == maximum
assert x + b == x + maximum
assert b + x == maximum + x
y = x
y += b
assert y == maximum + x
def test_subtraction(test_vals1, test_vals2):
val, minimum, maximum = test_vals1
x = test_vals2[0]
b = BoundedFloat(val, minimum, maximum)
assert b - x == val - x
assert x - float(b) == x - val
x = maximum * 2
b -= x
assert b == minimum
def test_multiplication(test_vals1, test_vals2):
val, minimum, maximum = test_vals1
x = test_vals2[0]
b = BoundedFloat(val, minimum, maximum)
assert b * x == val * x
assert x * b == x * val
b *= 2
assert b == val * 2 or (val * 2 > maximum and b == maximum)
b *= maximum + 1
assert b == maximum or (val == 0 and b == 0)
def test_division(test_vals1, test_vals2):
val, minimum, maximum = test_vals1
x = test_vals2[0]
b = BoundedFloat(val, minimum, maximum)
assert b / x == val / x
assert b // x == val // x
b /= x
assert val / x == b
def test_comparisons(test_vals1, test_vals2):
val, minimum, maximum = test_vals1
b = BoundedFloat(val, minimum, maximum)
x = test_vals2[0]
assert (b == x) == (val == x)
assert (x == b) == (x == val)
assert (b < x) == (val < x)
assert (b <= x) == (val <= x)
assert (x < b) == (x < val)
assert (x <= b) == (x <= val)
assert (b > x) == (val > x)
assert (b >= x) == (val >= x)
assert (x > b) == (x > val)
assert (x >= b) == (x >= val)
assert bool(b) == bool(val)
def test_conversion(test_vals1, test_vals2):
val, minimum, maximum = test_vals1
b = BoundedFloat(val, minimum, maximum)
x = int(b)
assert x == int(val)
assert isinstance(x, int)
y = float(b)
assert y == float(val)
if __name__ == "__main__":
test_vals1 = [(10, 0, 100), (0, 0, 1000), (100, 0, 100)]
test_vals2 = [(30, 3, 9), (1, 10, 100)]
for t1 in test_vals1:
for t2 in test_vals2:
test_addition(t1, t2)
test_subtraction(t1, t2)
test_multiplication(t1, t2)
test_division(t1, t2)
test_comparisons(t1, t2)
test_conversion(t1, t2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment