MCPcopy Index your code
hub / github.com/python/cpython / Rat

Class Rat

Lib/test/test_fractions.py:145–190  ·  view source on GitHub ↗

Simple Rational class for testing mixed arithmetic.

Source from the content-addressed store, hash-verified

143numbers.Complex.register(SymbolicComplex)
144
145class Rat:
146 """Simple Rational class for testing mixed arithmetic."""
147 def __init__(self, n, d):
148 self.numerator = n
149 self.denominator = d
150 def __mul__(self, other):
151 if isinstance(other, F):
152 return NotImplemented
153 return self.__class__(self.numerator * other.numerator,
154 self.denominator * other.denominator)
155 def __rmul__(self, other):
156 return self.__class__(other.numerator * self.numerator,
157 other.denominator * self.denominator)
158 def __truediv__(self, other):
159 if isinstance(other, F):
160 return NotImplemented
161 return self.__class__(self.numerator * other.denominator,
162 self.denominator * other.numerator)
163 def __rtruediv__(self, other):
164 return self.__class__(other.numerator * self.denominator,
165 other.denominator * self.numerator)
166 def __mod__(self, other):
167 if isinstance(other, F):
168 return NotImplemented
169 d = self.denominator * other.numerator
170 return self.__class__(self.numerator * other.denominator % d, d)
171 def __rmod__(self, other):
172 d = other.denominator * self.numerator
173 return self.__class__(other.numerator * self.denominator % d, d)
174
175 return self.__class__(other.numerator / self.numerator,
176 other.denominator / self.denominator)
177 def __pow__(self, other):
178 if isinstance(other, F):
179 return NotImplemented
180 return self.__class__(self.numerator ** other,
181 self.denominator ** other)
182 def __float__(self):
183 return self.numerator / self.denominator
184 def __eq__(self, other):
185 if self.__class__ != other.__class__:
186 return NotImplemented
187 return (typed_approx_eq(self.numerator, other.numerator) and
188 typed_approx_eq(self.denominator, other.denominator))
189 def __repr__(self):
190 return f'{self.__class__.__name__}({self.numerator!r}, {self.denominator!r})'
191numbers.Rational.register(Rat)
192
193class Root:

Callers 5

testFromNumberMethod · 0.70
testMixedDivisionMethod · 0.70
testMixedPowerMethod · 0.70

Calls

no outgoing calls

Tested by 5

testFromNumberMethod · 0.56
testMixedDivisionMethod · 0.56
testMixedPowerMethod · 0.56