@@ -401,7 +401,8 @@ def limit_denominator(self, max_denominator=1000000):
401401 # approximation to x if, and only if, it is a convergent or
402402 # semiconvergent of the (unique shortest) continued fraction
403403 # associated to x. See e.g. Continued Fractions by A. Ya.
404- # Khinchin (1964), Theorem 15.
404+ # Khinchin (1964), Theorem 15. See also
405+ # InternalDocs/fractions.md for details.
405406 #
406407 # To find a best rational approximation with denominator <= M,
407408 # we find the best upper and lower approximations with
@@ -723,72 +724,7 @@ def reverse(b, a):
723724 return forward , reverse
724725
725726 # Rational arithmetic algorithms: Knuth, TAOCP, Volume 2, 4.5.1.
726- #
727- # Assume input fractions a and b are normalized.
728- #
729- # 1) Consider addition/subtraction.
730- #
731- # Let g = gcd(da, db). Then
732- #
733- # na nb na*db ± nb*da
734- # a ± b == -- ± -- == ------------- ==
735- # da db da*db
736- #
737- # na*(db//g) ± nb*(da//g) t
738- # == ----------------------- == -
739- # (da*db)//g d
740- #
741- # Now, if g > 1, we're working with smaller integers.
742- #
743- # Note, that t, (da//g) and (db//g) are pairwise coprime.
744- #
745- # Indeed, (da//g) and (db//g) share no common factors (they were
746- # removed) and da is coprime with na (since input fractions are
747- # normalized), hence (da//g) and na are coprime. By symmetry,
748- # (db//g) and nb are coprime too. Then,
749- #
750- # gcd(t, da//g) == gcd(na*(db//g), da//g) == 1
751- # gcd(t, db//g) == gcd(nb*(da//g), db//g) == 1
752- #
753- # Above allows us optimize reduction of the result to lowest
754- # terms. Indeed,
755- #
756- # g2 = gcd(t, d) == gcd(t, (da//g)*(db//g)*g) == gcd(t, g)
757- #
758- # t//g2 t//g2
759- # a ± b == ----------------------- == ----------------
760- # (da//g)*(db//g)*(g//g2) (da//g)*(db//g2)
761- #
762- # is a normalized fraction. This is useful because the unnormalized
763- # denominator d could be much larger than g.
764- #
765- # We should special-case g == 1 (and g2 == 1), since 60.8% of
766- # randomly-chosen integers are coprime:
767- # https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality
768- # Note, that g2 == 1 always for fractions, obtained from floats: here
769- # g is a power of 2 and the unnormalized numerator t is an odd integer.
770- #
771- # 2) Consider multiplication
772- #
773- # Let g1 = gcd(na, db) and g2 = gcd(nb, da), then
774- #
775- # na*nb na*nb (na//g1)*(nb//g2)
776- # a*b == ----- == ----- == -----------------
777- # da*db db*da (db//g1)*(da//g2)
778- #
779- # Note, that after divisions we're multiplying smaller integers.
780- #
781- # Also, the resulting fraction is normalized, because each of
782- # two factors in the numerator is coprime to each of the two factors
783- # in the denominator.
784- #
785- # Indeed, pick (na//g1). It's coprime with (da//g2), because input
786- # fractions are normalized. It's also coprime with (db//g1), because
787- # common factors are removed by g1 == gcd(na, db).
788- #
789- # As for addition/subtraction, we should special-case g1 == 1
790- # and g2 == 1 for same reason. That happens also for multiplying
791- # rationals, obtained from floats.
727+ # See also InternalDocs/fractions.md for details.
792728
793729 def _add (a , b ):
794730 """a + b"""
0 commit comments