Python Forum
division error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: division error (/thread-18131.html)



division error - mcgrim - May-06-2019

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.


RE: division error - Gribouillis - May-06-2019

There are less than 52 lines, how could there be an error on line 52?


RE: division error - mcgrim - May-06-2019

it looks like that the error is contained within the command __truediv__,
therefore might not have anything to do directly with the code I wrote.


RE: division error - Gribouillis - May-06-2019

We need the full error message and we need the file name of the above code in order to see where it fails.


RE: division error - mcgrim - May-07-2019

this is everything that is printed out after I run it.

Error:
runfile('C:/UsersDesktop/python ode/extrial.py', wdir='C:/Users/Desktop/python ode') [-1, 3] [2, 6] [-4, -1] Traceback (most recent call last): File "<ipython-input-779-717f169c285d>", line 1, in <module> runfile('C:/Users/Desktop/python ode/extrial.py', wdir='C:/Users/Desktop/python ode') File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Desktop/python ode/extrial.py", line 111, in <module> print(I1/I2) 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



RE: division error - buran - May-07-2019

This code cannot possible run, because you class definition is with small i - class interval and you instantiate Interval

also what is homework2.py?


RE: division error - mcgrim - May-07-2019

solved, the problem was the capitalization.
Thanks a lot.
That is the name of the file.


RE: division error - buran - May-07-2019

(May-07-2019, 11:08 AM)mcgrim Wrote: That is the name of the file.
If that's the name of the file, what is extrial.py?


RE: division error - mcgrim - May-07-2019

sorry, I involuntarily created some confusion.
I have two different files next to each other, and I must have copy/pasted
from the other one.