Driver for class Rational. : Rational « Data Type « Python Tutorial






def gcd( x, y ):
   while y:
      z = x
      x = y
      y = z % y

   return x

class Rational:
   def __init__( self, top = 1, bottom = 1 ):
      if bottom == 0:
         raise ZeroDivisionError, "Cannot have 0 denominator"


      self.numerator = abs( top )
      self.denominator = abs( bottom )
      self.sign = ( top * bottom ) / ( self.numerator * self.denominator )

      self.simplify()  

   def simplify( self ):
      common = gcd( self.numerator, self.denominator )
      self.numerator /= common
      self.denominator /= common
         
   def __neg__( self ):
      return Rational( -self.sign * self.numerator, self.denominator )
      
   def __add__( self, other ):
      return Rational(self.sign * self.numerator * other.denominator +
         other.sign * other.numerator * self.denominator,
         self.denominator * other.denominator )

   def __sub__( self, other ):
      return self + ( -other )

   def __mul__( self, other ):
      return Rational( self.numerator * other.numerator,
                       self.sign * self.denominator *
                       other.sign * other.denominator )

   def __div__( self, other ):
      return Rational( self.numerator * other.denominator,
                       self.sign * self.denominator *
                       other.sign * other.numerator )

   def __truediv__( self, other ):
      return self.__div__( other )

   def __eq__( self, other ):
      return ( self - other ).numerator == 0

   def __lt__( self, other ):
      return ( self - other ).sign < 0

   def __gt__( self, other ):
      return ( self - other ).sign > 0

   def __le__( self, other ):
      return ( self < other ) or ( self == other )

   def __ge__( self, other ):
      return ( self > other ) or ( self == other )

   def __ne__( self, other ):
      return not ( self == other )      
   
   def __abs__( self ):
      return Rational( self.numerator, self.denominator )
   
   def __str__( self ):
      if self.sign == -1:
         signString = "-"
      else:
         signString = ""

      if self.numerator == 0:
         return "0"
      elif self.denominator == 1:
         return "%s%d" % ( signString, self.numerator )
      else:
         return "%s%d/%d" % ( signString, self.numerator, self.denominator )
            
   def __int__( self ):
      return self.sign * divmod( self.numerator, self.denominator )[ 0 ]

   def __float__( self ):
      return self.sign * float( self.numerator ) / self.denominator

   def __coerce__( self, other ):
      if type( other ) == type( 1 ):
         return ( self, Rational( other ) )
      else:
         return None

rational1 = Rational()  
rational2 = Rational( 10, 30 )  
rational3 = Rational( -7, 14 )  

print "rational1:", rational1
print "rational2:", rational2
print "rational3:", rational3
print

print rational1, "/", rational2, "=", rational1 / rational2
print rational3, "-", rational2, "=", rational3 - rational2
print rational2, "*", rational3, "-", rational1, "=", rational2 * rational3 - rational1

rational1 += rational2 * rational3
print "\nrational1 after adding rational2 * rational3:", rational1
print

print rational1, "<=", rational2, ":", rational1 <= rational2
print rational1, ">", rational3, ":", rational1 > rational3
print

print "The absolute value of", rational3, "is:", abs( rational3 )
print

print rational2, "as an integer is:", int( rational2 )
print rational2, "as a float is:", float( rational2 )
print rational2, "+ 1 =", rational2 + 1








2.15.Rational
2.15.1.Driver for class Rational.