Lines Matching refs:Fraction
4 """Fraction, infinite-precision, rational numbers."""
13 __all__ = ['Fraction']
38 class Fraction(numbers.Rational):
41 In the two-argument form of the constructor, Fraction(8, 6) will
44 defaults to 1 so that Fraction(3) == 3 and Fraction() == 0.
71 >>> Fraction(10, -8)
72 Fraction(-5, 4)
73 >>> Fraction(Fraction(1, 7), 5)
74 Fraction(1, 35)
75 >>> Fraction(Fraction(1, 7), Fraction(2, 3))
76 Fraction(3, 14)
77 >>> Fraction('314')
78 Fraction(314, 1)
79 >>> Fraction('-35/4')
80 Fraction(-35, 4)
81 >>> Fraction('3.1415') # conversion from numeric string
82 Fraction(6283, 2000)
83 >>> Fraction('-47e-2') # string may include a decimal exponent
84 Fraction(-47, 100)
85 >>> Fraction(1.47) # direct construction from float (exact conversion)
86 Fraction(6620291452234629, 4503599627370496)
87 >>> Fraction(2.25)
88 Fraction(9, 4)
89 >>> Fraction(Decimal('1.47'))
90 Fraction(147, 100)
93 self = super(Fraction, cls).__new__(cls)
115 raise ValueError('Invalid literal for Fraction: %r' %
157 raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
172 Beware that Fraction.from_float(0.3) != Fraction(3, 10).
198 Fraction and with a positive denominator.
203 """Closest Fraction to self with denominator at most max_denominator.
205 >>> Fraction('3.141592653589793').limit_denominator(10)
206 Fraction(22, 7)
207 >>> Fraction('3.141592653589793').limit_denominator(100)
208 Fraction(311, 99)
209 >>> Fraction(4321, 8765).limit_denominator(10000)
210 Fraction(4321, 8765)
237 return Fraction(self)
250 bound1 = Fraction(p0+k*p1, q0+k*q1)
251 bound2 = Fraction(p1, q1)
288 Fraction, that means that we define __add__ and __radd__ as:
293 if isinstance(other, (int, Fraction)):
294 return Fraction(self.numerator * other.denominator +
310 return Fraction(self.numerator * other.denominator +
321 Fraction. I'll refer to all of the above code that doesn't
322 refer to Fraction, float, or complex as "boilerplate". 'r'
323 will be an instance of Fraction, which is a subtype of
324 Rational (r : Fraction <: Rational), and b : B <:
327 1. If B <: Fraction, int, float, or complex, we handle
329 2. If Fraction falls back to the boilerplate code, and it
336 3. If B <: Fraction, Python tries B.__radd__ before
337 Fraction.__add__. This is ok, because it was
338 implemented with knowledge of Fraction, so it can
343 didn't know about Fraction in its implementation, and that it
358 if isinstance(b, (int, Fraction)):
458 return Fraction(na * db + da * nb, da * db, _normalize=False)
463 return Fraction(t, s * db, _normalize=False)
464 return Fraction(t // g2, s * (db // g2), _normalize=False)
474 return Fraction(na * db - da * nb, da * db, _normalize=False)
479 return Fraction(t, s * db, _normalize=False)
480 return Fraction(t // g2, s * (db // g2), _normalize=False)
496 return Fraction(na * nb, db * da, _normalize=False)
516 return Fraction(n, d, _normalize=False)
530 return div, Fraction(n_mod, da * db)
537 return Fraction((a.numerator * db) % (b.numerator * da), da * db)
553 return Fraction(a._numerator ** power,
557 return Fraction(a._denominator ** -power,
561 return Fraction((-a._denominator) ** -power,
578 return Fraction(a.numerator, a.denominator) ** b
586 """+a: Coerces a subclass instance to Fraction"""
587 return Fraction(a._numerator, a._denominator, _normalize=False)
591 return Fraction(-a._numerator, a._denominator, _normalize=False)
595 return Fraction(abs(a._numerator), a._denominator, _normalize=False)
638 # these operations will always be Fraction and therefore have
641 return Fraction(round(self * shift), shift)
643 return Fraction(round(self / shift) * shift)
648 # To make sure that the hash of a Fraction agrees with the hash
749 if type(self) == Fraction:
754 if type(self) == Fraction: