Lines Matching refs:Rat
25 """Test whether an object is an instance of the Rat class."""
26 return isinstance(x, Rat)
28 class Rat(object):
35 """Constructor: Rat([num[, den]]).
39 raise TypeError("Rat numerator must be int (%r)" % num)
41 raise TypeError("Rat denominator must be int (%r)" % den)
50 """Accessor function for read-only 'num' attribute of Rat."""
55 """Accessor function for read-only 'den' attribute of Rat."""
60 """Convert a Rat to a string resembling a Rat constructor call."""
61 return "Rat(%d, %d)" % (self.__num, self.__den)
64 """Convert a Rat to a string resembling a decimal numeric value."""
68 """Convert a Rat to a float."""
72 """Convert a Rat to an int; self.den must be 1."""
82 """Add two Rats, or a Rat and a number."""
84 other = Rat(other)
86 return Rat(self.__num*other.__den + other.__num*self.__den,
95 """Subtract two Rats, or a Rat and a number."""
97 other = Rat(other)
99 return Rat(self.__num*other.__den - other.__num*self.__den,
106 """Subtract two Rats, or a Rat and a number (reversed args)."""
108 other = Rat(other)
110 return Rat(other.__num*self.__den - self.__num*other.__den,
117 """Multiply two Rats, or a Rat and a number."""
119 return Rat(self.__num*other.__num, self.__den*other.__den)
121 return Rat(self.__num*other, self.__den)
129 """Divide two Rats, or a Rat and a number."""
131 return Rat(self.__num*other.__den, self.__den*other.__num)
133 return Rat(self.__num, self.__den*other)
139 """Divide two Rats, or a Rat and a number (reversed args)."""
141 return Rat(other.__num*self.__den, other.__den*self.__num)
143 return Rat(other*self.__den, self.__num)
151 other = Rat(other)
165 other = Rat(other)
174 other = Rat(other)
180 """Take one Rat modulo another."""
184 """Take one Rat modulo another (reversed args)."""
198 """Unit tests for Rat class and its support utilities."""
216 a = Rat(10, 15)
219 a = Rat(10, -15)
222 a = Rat(-10, 15)
225 a = Rat(-10, -15)
228 a = Rat(7)
232 a = Rat(1, 0)
236 self.fail("Rat(1, 0) didn't raise ZeroDivisionError")
237 for bad in "0", 0.0, 0j, (), [], {}, None, Rat, unittest:
239 a = Rat(bad)
243 self.fail("Rat(%r) didn't raise TypeError" % bad)
245 a = Rat(1, bad)
249 self.fail("Rat(1, %r) didn't raise TypeError" % bad)
252 self.assertEqual(Rat(2, 3) + Rat(1, 3), 1)
253 self.assertEqual(Rat(2, 3) + 1, Rat(5, 3))
254 self.assertEqual(1 + Rat(2, 3), Rat(5, 3))
255 self.assertEqual(1.0 + Rat(1, 2), 1.5)
256 self.assertEqual(Rat(1, 2) + 1.0, 1.5)
259 self.assertEqual(Rat(7, 2) - Rat(7, 5), Rat(21, 10))
260 self.assertEqual(Rat(7, 5) - 1, Rat(2, 5))
261 self.assertEqual(1 - Rat(3, 5), Rat(2, 5))
262 self.assertEqual(Rat(3, 2) - 1.0, 0.5)
263 self.assertEqual(1.0 - Rat(1, 2), 0.5)
266 self.assertEqual(Rat(2, 3) * Rat(5, 7), Rat(10, 21))
267 self.assertEqual(Rat(10, 3) * 3, 10)
268 self.assertEqual(3 * Rat(10, 3), 10)
269 self.assertEqual(Rat(10, 5) * 0.5, 1.0)
270 self.assertEqual(0.5 * Rat(10, 5), 1.0)
273 self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3))
274 self.assertEqual(Rat(10, 3) / 3, Rat(10, 9))
275 self.assertEqual(2 / Rat(5), Rat(2, 5))
276 self.assertEqual(3.0 * Rat(1, 2), 1.5)
277 self.assertEqual(Rat(1, 2) * 3.0, 1.5)
280 self.assertEqual(Rat(10) // Rat(4), 2)
281 self.assertEqual(Rat(10, 3) // Rat(4, 3), 2)
282 self.assertEqual(Rat(10) // 4, 2)
283 self.assertEqual(10 // Rat(4), 2)
286 self.assertEqual(Rat(10), Rat(20, 2))
287 self.assertEqual(Rat(10), 10)
288 self.assertEqual(10, Rat(10))
289 self.assertEqual(Rat(10), 10.0)
290 self.assertEqual(10.0, Rat(10))
293 self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3))
294 self.assertEqual(Rat(10, 3) / 3, Rat(10, 9))
295 self.assertEqual(2 / Rat(5), Rat(2, 5))
296 self.assertEqual(3.0 * Rat(1, 2), 1.5)
297 self.assertEqual(Rat(1, 2) * 3.0, 1.5)