

def gcd(a,b):
    while b: 
        a, b = b, a%b
    return a
    
class fraction:
    def __init__(self, num, den=1):
        if den < 0:
            num = -num
            den = -den
        divisor = gcd(num, den)
        self.num = num//divisor
        self.den = den//divisor
        
    def __repr__(self):
        return "%d/%d" % (self.num, self.den)
        
    def abc_repr(self):
        if self.num == 1:
            part_a = ""
        else:
            part_a = str(self.num)
            
        if self.den == 1:
            part_b = ""
        elif self.den == 2:
            part_b = "/"
        else:
            part_b = "/" + str(self.den)
            
        return part_a + part_b
        
    def multiple_of(self, other):
        return (self / other).den == 1
        
    def __add__(self, other):
        return fraction(self.num*other.den+other.num*self.den, self.den*other.den)
        
    def __sub__(self, other):
        return fraction(self.num*other.den-other.num*self.den, self.den*other.den)        
        
    def __mul__(self, other):
        return fraction(self.num*other.num, self.den*other.den)

    def __div__(self, other):
        return fraction(self.num*other.den, self.den*other.num)
        
    def __radd__(self, other):
        return fraction(self.num*other.den+other.num*self.den, self.den*other.den)
        
    def __rsub__(self, other):
        return fraction(other.num*self.den-self.num*other.den, self.den*other.den)        
        
    def __rmul__(self, other):
        return fraction(self.num*other.num, self.den*other.den)

    def __rdiv__(self, other):
        return fraction(other.num*self.den, other.den*self.num)
                
    __truediv__ = __div__
    
    def __cmp__(self, other):
        return cmp((self-other).num, 0)

    def __coerce__(self, other):
        if isinstance(other, fraction): 
            return self, other
        
        assert isinstance(other, int)
        return self, fraction(other, 1)
        
    def __float__(self):
        return float(self.num) / self.den



if __name__ == "__main__":
    print float(fraction(1,2))
    print fraction(0)

