May-09-2019, 07:55 PM
I am testing the function __truediv__ in this code, I am specifically interested in the
Is supposed to raise an error in case the division leads to + or - infinity.
This code instead raises the following error:
raise ValueErrorcommand.
Is supposed to raise an error in case the division leads to + or - infinity.
This code instead raises the following error:
Error: File "C:/Users/Desktop/python ode/homework22.py", line 53, in <module>
cc = aa/bb
TypeError: unsupported operand type(s) for /: 'interval' and 'interval'
I am not sure what is wrong, since everything else is working.class interval: def __init__(self,left,right): self.right=right self.left=left def __add__(self,other): a,b,c,d = self.left, self.right, other.left, other.right return interval(a+c,b+d) def __sub__(self,other): a,b,c,d=self.left,self.right,other.left,other.right return interval((a-d),(b-c)) def __mul__(self,other): a,b,c,d=self.left,self.right,other.left,other.right return interval(min(a*c, a*d, b*c, b*d), max(a*c, a*d, b*c, b*d)) def __div__(self, other): a, b, c, d = self.lo, self.up, other.lo, other.up # [c,d] cannot contain zero: if c*d <= 0: raise ValueError ('Interval %s cannot be denominator because it contains zero' % other) return interval(min(a/c, a/d, b/c, b/d), max(a/c, a/d, b/c, b/d)) def __repr__(self): return '[{},{}]'.format(self.left,self.right) I = interval aa = I(-3,-2) bb = I(0,5) cc = aa/bb print(aa,bb) print(cc)