Skip to content

Commit 8dc1ea9

Browse files
Move internal docs for fractions module to InternalDocs/fractions.md (#162475)
Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 828c153 commit 8dc1ea9

2 files changed

Lines changed: 74 additions & 67 deletions

File tree

‎InternalDocs/fractions.md‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,74 @@ require a potentially expensive `gcd` computation. We can use the
363363
In addition to the above proof, a formal, computer-verified proof
364364
of correctness (using Lean) of the algorithm can be found here:
365365
https://github.com/mdickinson/snippets/tree/1f47f8cc33185c646b679647e88cb24f29fd6a4e/proofs/limit_denominator
366+
367+
# Algorithms for rational arithmetic
368+
369+
In the following we assume that rational numbers $\frac{p}{q}$ are
370+
represented by fractions in lowest terms. That is, $p$ and $q$
371+
are coprime, $\gcd(p, q) = 1$. And, the denominator $q$ is positive.
372+
373+
Consider fractions $a/b$ and $c/d$.
374+
375+
## Addition/subtraction
376+
377+
Let $g = \gcd(b, d)$. Then\
378+
$$\frac{a}{b} \pm \frac{c}{d} = \frac{a d \pm b c}{b d}
379+
=\frac{a (d/g) \pm c (b/g)}{(b d)/g}
380+
=\frac{p}{q}$$
381+
382+
Now, if $g > 1$, we're working with smaller integers. Also,
383+
$p$, $b/g$, $d/g$ are pairwise coprime.
384+
385+
Indeed, $b/g$ and $d/g$ share no common factors (they were
386+
removed) and $b$ is coprime with $a$ (since input fractions are
387+
in lowest terms), hence $b/g$ and $a$ are coprime. By symmetry,
388+
$d/g$ and $c$ are coprime too. Then\
389+
$$\gcd(p, b/g)
390+
=\gcd(a (d/g) \pm c (b/g), b/g)
391+
=\gcd(a (d/g), b/g) = 1$$
392+
393+
and similarly\
394+
$$\gcd(p, d/g) = \gcd(c(b/g), d/g) = 1$$
395+
396+
The above allows us to optimize reduction of the result to lowest
397+
terms. Indeed,\
398+
$$h = \gcd(p, q)
399+
=\gcd(p, (b/g)(d/g) g)
400+
=\gcd(p, g)$$
401+
402+
and\
403+
$$\frac{a}{b}\pm\frac{c}{d}
404+
=\frac{p/h}{(b/g)(d/g)(g/h)}
405+
=\frac{(a (d/g) \pm c (b/g) )/h}
406+
{(b/g)(d/h)}$$
407+
408+
is a normalized fraction. This is useful because the unnormalized
409+
denominator $q$ could be much larger than $g$.
410+
411+
We should special-case $g = 1$ and $h = 1$, since 61% of randomly-chosen pairs of integers
412+
[are coprime](https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality).
413+
Note that the second case always holds for fractions, obtained from floats: here
414+
$g$ is a power of 2 and the unnormalized numerator $p$ is an odd integer.
415+
416+
## Multiplication
417+
418+
Let $g = \gcd(a, d)$ and $h = \gcd(b, c)$, then\
419+
$$\frac{a}{b}\times\frac{c}{d}
420+
=\frac{(a/g)(c/h)}
421+
{(d/g)(b/h)}$$
422+
423+
Note that after divisions we're multiplying smaller integers.
424+
425+
Also, the resulting fraction is normalized, because each of
426+
two factors in the numerator is coprime to each of the two factors
427+
in the denominator.
428+
429+
Indeed, pick $a/g$. It's coprime with $b/h$, because input
430+
fractions are in lowest terms. It's also coprime with $d/g$,
431+
because common factors are removed by $g = \gcd(a, d)$.
432+
By symmetry, $c/h$ is also coprime with both factors in the denominator.
433+
434+
As for addition/subtraction, we should special-case $g = 1$
435+
and $h = 1$ for the same reason. That happens also for multiplying
436+
rationals, obtained from floats.

‎Lib/fractions.py‎

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)