Driver for class Rational. : Rational : Data Type PYTHON TUTORIALS


PYTHON TUTORIALS » Data Type » Rational »

 

Driver for class Rational.


def gcdx, y ):
   while y:
      z = x
      x = y
      y = z % y

   return x

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


      self.numerator = abstop )
      self.denominator = absbottom )
      self.sign = top * bottom self.numerator * self.denominator )

      self.simplify()  

   def simplifyself ):
      common = gcdself.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 Rationalself.numerator * other.numerator,
                       self.sign * self.denominator *
                       other.sign * other.denominator )

   def __div__self, other ):
      return Rationalself.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 Rationalself.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 * divmodself.numerator, self.denominator )[ ]

   def __float__self ):
      return self.sign * floatself.numerator / self.denominator

   def __coerce__self, other ):
      if typeother == type):
         return self, Rationalother ) )
      else:
         return None

rational1 = Rational()  
rational2 = Rational1030 )  
rational3 = Rational-714 )  

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:", absrational3 )
print

print rational2, "as an integer is:"intrational2 )
print rational2, "as a float is:"floatrational2 )
print rational2, "+ 1 =", rational2 + 1



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Data Type
» Rational