From 52b42b2fdda4623ff395c663bb269c9402716428 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 23 Nov 2026 08:41:01 +0300 Subject: [PATCH 1/4] Move internal docs for fractions module to InternalDocs/fractions.md This covers algorithms for addition/subtraction/multiplication. --- InternalDocs/fractions.md | 72 +++++++++++++++++++++++++++++++++++++++ Lib/fractions.py | 66 ----------------------------------- 2 files changed, 72 insertions(+), 66 deletions(-) diff --git a/InternalDocs/fractions.md b/InternalDocs/fractions.md index a01950f5778648..9ac9716ed8d466 100644 --- a/InternalDocs/fractions.md +++ b/InternalDocs/fractions.md @@ -363,3 +363,75 @@ require a potentially expensive `gcd` computation. We can use the In addition to the above proof, a formal, computer-verified proof of correctness (using Lean) of the algorithm can be found here: https://github.com/mdickinson/snippets/tree/1f47f8cc33185c646b679647e88cb24f29fd6a4e/proofs/limit_denominator + +# Algorithms for rational arithmetics + +In following we assume, that rational numbers $\frac{p}{q}$ are +represented by fractions in lowest terms. I.e. $p$ and $q$ +are coprime, $\gcd(p, q) = 1$. Also, denominator $q$ is positive. + +Consider fractions $a/b$ and $c/d$. + +## Addition/subtraction + +Let $g = \gcd(b, d)$. Then\ +$$\frac{a}{b} \pm \frac{c}{d} = \frac{a d \pm b c}{b d} +=\frac{a \left(d/g\right) \pm c \left(b/g\right)}{\left(b d\right)/g} +=\frac{p}{q}$$ + +Now, if $g > 1$, we're working with smaller integers. Also, +$p$, $b/g$, $d/g$ are pairwise coprime. + +Indeed, $b/g$ and $d/g$ share no common factors (they were +removed) and $b$ is coprime with $a$ (since input fractions are +in lowest terms), hence $b/g$ and $a$ are coprime. By symmetry, +$d/g$ and $c$ are coprime too. Then\ +$$\gcd\left(p, b/g\right) +=\gcd\left(a \left(d/g\right) \pm c \left(b/g\right), b/g\right) +=\gcd\left(a \left(d/g\right), b/g\right) = 1$$ + +and similary\ +$$\gcd\left(p, d/g\right) = \gcd\left(c\left(b/g\right), d/g\right) = 1$$ + +Above allows us optimize reduction of the result to lowest +terms. Indeed,\ +$$h = \gcd\left(p, q\right) +=\gcd\left(p, \left(b/g\right)\left(d/g\right) g\right) +=\gcd\left(p, g\right)$$ + +and\ +$$\frac{a}{b}\pm\frac{c}{d} +=\frac{p/h}{\left(b/g\right)\left(d/g\right)\left(g/h\right)} +=\frac{\left(a \left(d/g\right) \pm c \left(b/g\right) \right)/h} +{\left(b/g\right)\left(d/h\right)}$$ + +is a normalized fraction. This is useful because the unnormalized +denominator $q$ could be much larger than $g$. + +We should special-case $g = 1$ and $h = 1$, since 61% of randomly-chosen +integers [are +coprime](https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality). +Note, that second case always holds for fractions, obtained from floats: here +$g$ is a power of 2 and the unnormalized numerator $p$ is an odd integer. + +## Multiplication + +Let $g = \gcd(a, d)$ and $h = \gcd\left(b, c\right)$, then\ +$$\frac{a}{b}\times\frac{c}{d} +=\frac{\left(a/g\right)\left(c/h\right)} +{\left(d/g\right)\left(b/h\right)}$$ + +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 $a/g$. It's coprime with $b/h$, because input +fractions are in lowest terms. It's also coprime with $d/g$, +because common factors are removed by $g = \gcd\left(a, d\right)$. +By symmetry, $c/h$ also coprime with both factors in the denominator. + +As for addition/subtraction, we should special-case $g = 1$ +and $h = 1$ for same reason. That happens also for multiplying +rationals, obtained from floats. diff --git a/Lib/fractions.py b/Lib/fractions.py index e7c5105a90aeb1..72bf11a5f1dce3 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -725,72 +725,6 @@ def reverse(b, a): return forward, reverse # Rational arithmetic algorithms: Knuth, TAOCP, Volume 2, 4.5.1. - # - # 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. def _add(a, b): """a + b""" From 6e3843b39f3ec4357e2a60f96308ca30dca7310c Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 24 Nov 2026 12:34:02 +0300 Subject: [PATCH 2/4] address review: references to InternalDocs/fractions.md --- Lib/fractions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/fractions.py b/Lib/fractions.py index 72bf11a5f1dce3..325cc905eeca20 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -403,7 +403,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. # # To find a best rational approximation with denominator <= M, # we find the best upper and lower approximations with @@ -725,6 +726,7 @@ def reverse(b, a): return forward, reverse # Rational arithmetic algorithms: Knuth, TAOCP, Volume 2, 4.5.1. + # See also InternalDocs/fractions.md for details. def _add(a, b): """a + b""" From 0f20557c27586d4e6f089030cf27be320fd123be Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 24 Nov 2026 14:07:17 +0300 Subject: [PATCH 3/4] Apply batched suggestions from code review Co-authored-by: Stan Ulbrych --- InternalDocs/fractions.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/InternalDocs/fractions.md b/InternalDocs/fractions.md index 9ac9716ed8d466..174eccf058a105 100644 --- a/InternalDocs/fractions.md +++ b/InternalDocs/fractions.md @@ -364,11 +364,11 @@ In addition to the above proof, a formal, computer-verified proof of correctness (using Lean) of the algorithm can be found here: https://github.com/mdickinson/snippets/tree/1f47f8cc33185c646b679647e88cb24f29fd6a4e/proofs/limit_denominator -# Algorithms for rational arithmetics +# Algorithms for rational arithmetic -In following we assume, that rational numbers $\frac{p}{q}$ are -represented by fractions in lowest terms. I.e. $p$ and $q$ -are coprime, $\gcd(p, q) = 1$. Also, denominator $q$ is positive. +In following we assume that rational numbers $\frac{p}{q}$ are +represented by fractions in lowest terms. That is, $p$ and $q$ +are coprime, $\gcd(p, q) = 1$. And, the denominator $q$ is positive. Consider fractions $a/b$ and $c/d$. @@ -390,7 +390,7 @@ $$\gcd\left(p, b/g\right) =\gcd\left(a \left(d/g\right) \pm c \left(b/g\right), b/g\right) =\gcd\left(a \left(d/g\right), b/g\right) = 1$$ -and similary\ +and similarly\ $$\gcd\left(p, d/g\right) = \gcd\left(c\left(b/g\right), d/g\right) = 1$$ Above allows us optimize reduction of the result to lowest @@ -410,8 +410,8 @@ denominator $q$ could be much larger than $g$. We should special-case $g = 1$ and $h = 1$, since 61% of randomly-chosen integers [are -coprime](https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality). -Note, that second case always holds for fractions, obtained from floats: here +integers [are coprime](https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality). +Note that second case always holds for fractions, obtained from floats: here $g$ is a power of 2 and the unnormalized numerator $p$ is an odd integer. ## Multiplication @@ -421,7 +421,7 @@ $$\frac{a}{b}\times\frac{c}{d} =\frac{\left(a/g\right)\left(c/h\right)} {\left(d/g\right)\left(b/h\right)}$$ -Note, that after divisions we're multiplying smaller integers. +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 @@ -430,8 +430,8 @@ in the denominator. Indeed, pick $a/g$. It's coprime with $b/h$, because input fractions are in lowest terms. It's also coprime with $d/g$, because common factors are removed by $g = \gcd\left(a, d\right)$. -By symmetry, $c/h$ also coprime with both factors in the denominator. +By symmetry, $c/h$ is also coprime with both factors in the denominator. As for addition/subtraction, we should special-case $g = 1$ -and $h = 1$ for same reason. That happens also for multiplying +and $h = 1$ for the same reason. That happens also for multiplying rationals, obtained from floats. From 4a9b7057d6604391c8225839306dd2362d34f00a Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 25 Nov 2026 05:25:24 +0300 Subject: [PATCH 4/4] address review: Above -> The above --- InternalDocs/fractions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InternalDocs/fractions.md b/InternalDocs/fractions.md index 174eccf058a105..39a0756c52f060 100644 --- a/InternalDocs/fractions.md +++ b/InternalDocs/fractions.md @@ -393,7 +393,7 @@ $$\gcd\left(p, b/g\right) and similarly\ $$\gcd\left(p, d/g\right) = \gcd\left(c\left(b/g\right), d/g\right) = 1$$ -Above allows us optimize reduction of the result to lowest +The above allows us optimize reduction of the result to lowest terms. Indeed,\ $$h = \gcd\left(p, q\right) =\gcd\left(p, \left(b/g\right)\left(d/g\right) g\right)