0

I am writing a program to divide rational number but I want it to be able to handle fractions. I want to divide 1 by 1/3 but my program is encountering an error when dealing with the integer. I have tried to convert the integer to a rational several different ways but nothing works. any help or guidance will be much appreciated.

Here's the error I keep receiving it is a flag from an assert statement at the bottom of the code.

Traceback (most recent call last): File "E:\Python\Rational number extension excercise.py", line 47, in assert Rational(3) == 1 / r3, "Division test failed." TypeError: unsupported operand type(s) for /: 'int' and 'Rational'

class Rational(object):
 """ Rational with numerator and denominator. Denominator
 parameter defaults to 1"""

 def __init__(self,numer,denom=1):  
     #test print('in constructor')
        self.numer = numer
        self.denom = denom

 def __truediv__(self,param):
    '''divide two rationals'''
    #test print('in truediv')
    if type(param) == int:  # convert ints to Rationals
        param = Rational(param)
    if type(param) == Rational:
        # find a common denominator (lcm)
        the_lcm = lcm(self.denom, param.numer)
        # adjust the param value
        lcm_numer = (the_lcm * param.numer)
        lcm_denom = (the_lcm * param.denom)
        true_param = int(lcm_denom / lcm_numer)
        #print(int(lcm_denom / lcm_numer))
        # multiply each by the lcm, then multiply
        numerator_sum = (the_lcm * self.numer/self.denom) * (true_param)
        #print(numerator_sum)
        #print(Rational(int(numerator_sum),the_lcm))
        return Rational(int(numerator_sum),the_lcm)
    else:
        print('wrong type')  # problem: some type we cannot handle
        raise(TypeError)

 def __rdiv__(self,param):
    '''divide two reversed rationals'''
    # mapping is correct: if "(1) / (x/x)", 1 maps (to 1/1)
    if type(self) == int:
        self.numer = self
        self.denom = 1
    return self.__truediv__(self.numer)
    return self.__truediv__(self.denom)

r1 = Rational(2,3)
r2 = Rational(1,4)
r3 = Rational(1,3)

assert Rational(2) == r1 / r3, "Division test failed."
assert Rational(3) == 1 / r3, "Division test failed."
John Anthony
  • 77
  • 2
  • 8
  • 1
    Could you provide a failing test case? What do you mean by "having trouble"? – jonrsharpe Apr 07 '14 at 13:36
  • 1
    Which version of `Python` are you using? You have `3.x` tagged. In recent versions of Python (`3.x`) dividing two `int`s will result in a `float` automatically. In your case, dividing an `int` by a `float` should also result in a `float` due to type promotion. Can you provide an example of the problem you are seeing? – Cory Kramer Apr 07 '14 at 13:37
  • by "having trouble" I mean that the assert statement in the program is not letting me divide an integer (one in the example) by a rational number (1/3 in the example). I am using python 3.3.4. type promotion is not used in the program because I am dividing the numbers as if they are fractions ex. (3/3) / (1/ 3) = 3 – John Anthony Apr 07 '14 at 19:42

1 Answers1

2
 def __rdiv__(self,param):
    '''divide two reversed rationals'''
    # mapping is correct: if "(1) / (x/x)", 1 maps (to 1/1)
    if type(self) == int:
        self.numer = self
        self.denom = 1

type(self) == int will never evaluate to True: if you're running __rdiv__ on the Racional, self will always be a Racional. You'll want to test param instead, which is the left side of the division (1, in your example).

loopbackbee
  • 21,962
  • 10
  • 62
  • 97