Home | History | Annotate | Download | only in test

Lines Matching refs:Rat

24     """Test wheter an object is an instance of the Rat class."""
25 return isinstance(x, Rat)
27 class Rat(object):
34 """Constructor: Rat([num[, den]]).
38 raise TypeError, "Rat numerator must be int or long (%r)" % num
40 raise TypeError, "Rat denominator must be int or long (%r)" % den
49 """Accessor function for read-only 'num' attribute of Rat."""
54 """Accessor function for read-only 'den' attribute of Rat."""
59 """Convert a Rat to an string resembling a Rat constructor call."""
60 return "Rat(%d, %d)" % (self.__num, self.__den)
63 """Convert a Rat to a string resembling a decimal numeric value."""
67 """Convert a Rat to a float."""
71 """Convert a Rat to an int; self.den must be 1."""
81 """Convert a Rat to an long; self.den must be 1."""
87 """Add two Rats, or a Rat and a number."""
89 other = Rat(other)
91 return Rat(self.__num*other.__den + other.__num*self.__den,
100 """Subtract two Rats, or a Rat and a number."""
102 other = Rat(other)
104 return Rat(self.__num*other.__den - other.__num*self.__den,
111 """Subtract two Rats, or a Rat and a number (reversed args)."""
113 other = Rat(other)
115 return Rat(other.__num*self.__den - self.__num*other.__den,
122 """Multiply two Rats, or a Rat and a number."""
124 return Rat(self.__num*other.__num, self.__den*other.__den)
126 return Rat(self.__num*other, self.__den)
134 """Divide two Rats, or a Rat and a number."""
136 return Rat(self.__num*other.__den, self.__den*other.__num)
138 return Rat(self.__num, self.__den*other)
146 """Divide two Rats, or a Rat and a number (reversed args)."""
148 return Rat(other.__num*self.__den, other.__den*self.__num)
150 return Rat(other*self.__den, self.__num)
160 other = Rat(other)
174 other = Rat(other)
183 other = Rat(other)
189 """Take one Rat modulo another."""
193 """Take one Rat modulo another (reversed args)."""
214 """Unit tests for Rat class and its support utilities."""
232 a = Rat(10, 15)
235 a = Rat(10L, 15L)
238 a = Rat(10, -15)
241 a = Rat(-10, 15)
244 a = Rat(-10, -15)
247 a = Rat(7)
251 a = Rat(1, 0)
255 self.fail("Rat(1, 0) didn't raise ZeroDivisionError")
256 for bad in "0", 0.0, 0j, (), [], {}, None, Rat, unittest:
258 a = Rat(bad)
262 self.fail("Rat(%r) didn't raise TypeError" % bad)
264 a = Rat(1, bad)
268 self.fail("Rat(1, %r) didn't raise TypeError" % bad)
271 self.assertEqual(Rat(2, 3) + Rat(1, 3), 1)
272 self.assertEqual(Rat(2, 3) + 1, Rat(5, 3))
273 self.assertEqual(1 + Rat(2, 3), Rat(5, 3))
274 self.assertEqual(1.0 + Rat(1, 2), 1.5)
275 self.assertEqual(Rat(1, 2) + 1.0, 1.5)
278 self.assertEqual(Rat(7, 2) - Rat(7, 5), Rat(21, 10))
279 self.assertEqual(Rat(7, 5) - 1, Rat(2, 5))
280 self.assertEqual(1 - Rat(3, 5), Rat(2, 5))
281 self.assertEqual(Rat(3, 2) - 1.0, 0.5)
282 self.assertEqual(1.0 - Rat(1, 2), 0.5)
285 self.assertEqual(Rat(2, 3) * Rat(5, 7), Rat(10, 21))
286 self.assertEqual(Rat(10, 3) * 3, 10)
287 self.assertEqual(3 * Rat(10, 3), 10)
288 self.assertEqual(Rat(10, 5) * 0.5, 1.0)
289 self.assertEqual(0.5 * Rat(10, 5), 1.0)
292 self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3))
293 self.assertEqual(Rat(10, 3) / 3, Rat(10, 9))
294 self.assertEqual(2 / Rat(5), Rat(2, 5))
295 self.assertEqual(3.0 * Rat(1, 2), 1.5)
296 self.assertEqual(Rat(1, 2) * 3.0, 1.5)
299 self.assertEqual(Rat(10) // Rat(4), 2)
300 self.assertEqual(Rat(10, 3) // Rat(4, 3), 2)
301 self.assertEqual(Rat(10) // 4, 2)
302 self.assertEqual(10 // Rat(4), 2)
305 self.assertEqual(Rat(10), Rat(20, 2))
306 self.assertEqual(Rat(10), 10)
307 self.assertEqual(10, Rat(10))
308 self.assertEqual(Rat(10), 10.0)
309 self.assertEqual(10.0, Rat(10))
318 self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3))
319 self.assertEqual(Rat(10, 3) / 3, Rat(10, 9))
320 self.assertEqual(2 / Rat(5), Rat(2, 5))
321 self.assertEqual(3.0 * Rat(1, 2), 1.5)
322 self.assertEqual(Rat(1, 2) * 3.0, 1.5)