Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
>>> Interval(-1, 5) <= Interval(6, 7)
Interval(low=1, high=1)
>>> Interval(-1, 5) <= Interval(5, 7)
Interval(low=1, high=1)
>>> Interval(-1, 5) <= Interval(-16, -7)
Interval(low=0, high=0)
>>> Interval(1, 5) <= Interval(3, 7)
Interval(low=0, high=1)
"""
if self.high <= other.low:
return Interval(1, 1)
if self.low > other.high:
return Interval(0, 0)
return Interval(0, 1)
def union(self, other):
if isinstance(other, Interval):
return UNKNOWN_RANGE
return IntervalTuple(x.union(y) for x, y in zip(self.values,
other.values))
def cmp_values(_):
""" Return the range of a comparison value, i.e. [-1, 1]. """
return Interval(-1, 1)
def widen(self, other):
""" Widen current range. """
if self.low < other.low:
low = -float("inf")
else:
low = self.low
if self.high > other.high:
high = float("inf")
else:
high = self.high
return Interval(low, high)
def __sub__(self, other):
"""
Combiner for Subtraction operation.
>>> Interval(1, 5) - Interval(-5, -4)
Interval(low=5, high=10)
"""
sl, sh, ol, oh = self.low, self.high, other.low, other.high
if isinf(sl) and isinf(oh):
return UNKNOWN_RANGE
if isinf(sh) and isinf(ol):
return UNKNOWN_RANGE
return Interval(sl - oh, sh - ol)
def __mod__(range1, range2):
""" Combiner for Modulo operation.
>>> Interval(-1, 5) % Interval(1, 13)
Interval(low=0, high=5)
>>> Interval(-21, 5) % Interval(1, 13)
Interval(low=0, high=13)
"""
return Interval(0, min(range2.high,
max(abs(range1.high), abs(range1.low))))
>>> Interval(-1, 5) < Interval(6, 7)
Interval(low=1, high=1)
>>> Interval(-1, 5) < Interval(5, 7)
Interval(low=0, high=1)
>>> Interval(-1, 5) < Interval(-16, -7)
Interval(low=0, high=0)
>>> Interval(1, 5) < Interval(3, 7)
Interval(low=0, high=1)
"""
if self.high < other.low:
return Interval(1, 1)
if self.low > other.high:
return Interval(0, 0)
return Interval(0, 1)
Combiner for lower than or equal operation.
>>> Interval(-1, 5) <= Interval(6, 7)
Interval(low=1, high=1)
>>> Interval(-1, 5) <= Interval(5, 7)
Interval(low=1, high=1)
>>> Interval(-1, 5) <= Interval(-16, -7)
Interval(low=0, high=0)
>>> Interval(1, 5) <= Interval(3, 7)
Interval(low=0, high=1)
"""
if self.high <= other.low:
return Interval(1, 1)
if self.low > other.high:
return Interval(0, 0)
return Interval(0, 1)
def positive_values(_):
""" Return a positive range without upper bound. """
return Interval(0, float("inf"))
Combiner for greater than or equal operation.
>>> Interval(-5, 1) >= Interval(-7, -6)
Interval(low=1, high=1)
>>> Interval(-5, 1) >= Interval(-7, -5)
Interval(low=1, high=1)
>>> Interval(-1, 5) >= Interval(6, 7)
Interval(low=0, high=0)
>>> Interval(1, 5) >= Interval(3, 7)
Interval(low=0, high=1)
"""
if self.low >= other.high:
return Interval(1, 1)
if self.high < other.low:
return Interval(0, 0)
return Interval(0, 1)