Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
division error
#1
The following code

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(min(a,b)-max(c,d),max(a,b)-min(c,d))
    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:
        print(min(a/c, a/d, b/c, b/d))
        
        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)

I1 = Interval(1, 4)
I2 = Interval(-2, -1)
print(I1 + I2)       
print(I1-I2)
print(I1*I2)
print(I1/I2)
gives me the following error:

Error:
File "C:/Users/Desktop/python ode/homework2.py", line 52, in __truediv__ raise ValueError('Please make sure values is of sort int and x !=0') ValueError: Please make sure values is of sort int and x !=0
and I am not sure what to do to fix it.
Reply


Messages In This Thread
division error - by mcgrim - May-06-2019, 09:25 PM
RE: division error - by Gribouillis - May-06-2019, 09:36 PM
RE: division error - by mcgrim - May-06-2019, 09:40 PM
RE: division error - by Gribouillis - May-06-2019, 10:27 PM
RE: division error - by mcgrim - May-07-2019, 08:04 AM
RE: division error - by buran - May-07-2019, 10:51 AM
RE: division error - by mcgrim - May-07-2019, 11:08 AM
RE: division error - by buran - May-07-2019, 11:31 AM
RE: division error - by mcgrim - May-07-2019, 12:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  1 dimension Matrix Right Division theo_taison1 1 4,013 Jan-25-2017, 04:51 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020