class Interval: """ This class illustrates basic interval arithmetic. """ def __init__(self,a,*b): "returns the interval (a,b)" self.a = float(a) if len(b) == 0: self.b = float(a) else: self.b = float(b[0]) def __str__(self): "return string representation" s = '[%f,%f]' % (self.a,self.b) return s def __repr__(self): "calls the string representation" return str(self) def __add__(self,other): "returns addition of two intervals" x = self.a + other.a y = self.b + other.b return Interval(x,y) def __sub__(self,other): "return subtraction of two intervals" x = self.a - other.b y = self.b - other.a return Interval(x,y) def __mul__(self,other): "returns multiplication of two intervals" ac = self.a*other.a ad = self.a*other.b bc = self.b*other.a bd = self.b*other.b L = [ac,ad,bc,bd] return Interval(min(L),max(L)) def __div__(self,other): "returns division of two intervals" ac = self.a/other.a ad = self.a/other.b bc = self.b/other.a bd = self.b/other.b L = [ac,ad,bc,bd] return Interval(min(L),max(L))