Repository navigation
Move internal docs for fractions module to InternalDocs/fractions.md #162475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
52b42b2
9048d40
6e3843b
0f20557
b901964
4a9b705
dee63c6
a04a798
ae566a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -401,7 +401,8 @@ def limit_denominator(self, max_denominator=1000000): | |||||||
| # approximation to x if, and only if, it is a convergent or | ||||||||
| # semiconvergent of the (unique shortest) continued fraction | ||||||||
| # associated to x. See e.g. Continued Fractions by A. Ya. | ||||||||
| # Khinchin (1964), Theorem 15. | ||||||||
| # Khinchin (1964), Theorem 15. See also | ||||||||
| # InternalDocs/fractions.md for details. | ||||||||
|
Comment on lines
+404
to
+405
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I would prefer to revert this, however. Comments in the module are |
||||||||
| # | ||||||||
| # To find a best rational approximation with denominator <= M, | ||||||||
| # we find the best upper and lower approximations with | ||||||||
|
|
@@ -723,72 +724,7 @@ def reverse(b, a): | |||||||
| return forward, reverse | ||||||||
|
|
||||||||
| # Rational arithmetic algorithms: Knuth, TAOCP, Volume 2, 4.5.1. | ||||||||
|
StanFromIreland marked this conversation as resolved.
Resolved
|
||||||||
| # | ||||||||
| # Assume input fractions a and b are normalized. | ||||||||
| # | ||||||||
| # 1) Consider addition/subtraction. | ||||||||
| # | ||||||||
| # Let g = gcd(da, db). Then | ||||||||
| # | ||||||||
| # na nb na*db ± nb*da | ||||||||
| # a ± b == -- ± -- == ------------- == | ||||||||
| # da db da*db | ||||||||
| # | ||||||||
| # na*(db//g) ± nb*(da//g) t | ||||||||
| # == ----------------------- == - | ||||||||
| # (da*db)//g d | ||||||||
| # | ||||||||
| # Now, if g > 1, we're working with smaller integers. | ||||||||
| # | ||||||||
| # Note, that t, (da//g) and (db//g) are pairwise coprime. | ||||||||
| # | ||||||||
| # Indeed, (da//g) and (db//g) share no common factors (they were | ||||||||
| # removed) and da is coprime with na (since input fractions are | ||||||||
| # normalized), hence (da//g) and na are coprime. By symmetry, | ||||||||
| # (db//g) and nb are coprime too. Then, | ||||||||
| # | ||||||||
| # gcd(t, da//g) == gcd(na*(db//g), da//g) == 1 | ||||||||
| # gcd(t, db//g) == gcd(nb*(da//g), db//g) == 1 | ||||||||
| # | ||||||||
| # Above allows us optimize reduction of the result to lowest | ||||||||
| # terms. Indeed, | ||||||||
| # | ||||||||
| # g2 = gcd(t, d) == gcd(t, (da//g)*(db//g)*g) == gcd(t, g) | ||||||||
| # | ||||||||
| # t//g2 t//g2 | ||||||||
| # a ± b == ----------------------- == ---------------- | ||||||||
| # (da//g)*(db//g)*(g//g2) (da//g)*(db//g2) | ||||||||
| # | ||||||||
| # is a normalized fraction. This is useful because the unnormalized | ||||||||
| # denominator d could be much larger than g. | ||||||||
| # | ||||||||
| # We should special-case g == 1 (and g2 == 1), since 60.8% of | ||||||||
| # randomly-chosen integers are coprime: | ||||||||
| # https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality | ||||||||
| # Note, that g2 == 1 always for fractions, obtained from floats: here | ||||||||
| # g is a power of 2 and the unnormalized numerator t is an odd integer. | ||||||||
| # | ||||||||
| # 2) Consider multiplication | ||||||||
| # | ||||||||
| # Let g1 = gcd(na, db) and g2 = gcd(nb, da), then | ||||||||
| # | ||||||||
| # na*nb na*nb (na//g1)*(nb//g2) | ||||||||
| # a*b == ----- == ----- == ----------------- | ||||||||
| # da*db db*da (db//g1)*(da//g2) | ||||||||
| # | ||||||||
| # Note, that after divisions we're multiplying smaller integers. | ||||||||
| # | ||||||||
| # Also, the resulting fraction is normalized, because each of | ||||||||
| # two factors in the numerator is coprime to each of the two factors | ||||||||
| # in the denominator. | ||||||||
| # | ||||||||
| # Indeed, pick (na//g1). It's coprime with (da//g2), because input | ||||||||
| # fractions are normalized. It's also coprime with (db//g1), because | ||||||||
| # common factors are removed by g1 == gcd(na, db). | ||||||||
| # | ||||||||
| # As for addition/subtraction, we should special-case g1 == 1 | ||||||||
| # and g2 == 1 for same reason. That happens also for multiplying | ||||||||
| # rationals, obtained from floats. | ||||||||
| # See also InternalDocs/fractions.md for details. | ||||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here.
Suggested change
|
||||||||
|
|
||||||||
| def _add(a, b): | ||||||||
| """a + b""" | ||||||||
|
|
||||||||
Uh oh!
There was an error while loading. Please reload this page.