Skip to content

Commit 998d0af

Browse files
committed
Improve comments, remove unneeded case.
1 parent 16908fb commit 998d0af

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

Lib/_pylong.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ def str_to_int(s):
164164

165165

166166
# Fast integer division, based on code from Mark Dickinson, fast_div.py
167-
# GH-47701. The algorithm is due to Burnikel and Ziegler, in their paper
168-
# "Fast Recursive Division".
167+
# GH-47701. Additional refinements and optimizations by Bjorn Martinsson. The
168+
# algorithm is due to Burnikel and Ziegler, in their paper "Fast Recursive
169+
# Division".
169170

170171
_DIV_LIMIT = 1000
171172

@@ -214,7 +215,18 @@ def _div3n2n(a12, a3, b, b1, b2, n):
214215

215216

216217
def _int2digits(a, n):
217-
"""decompose non-negative integer a into base 2**n"""
218+
"""Decompose non-negative int a into base 2**n
219+
220+
Input:
221+
a is a non-negative integer
222+
223+
Output:
224+
List of the digits of a in base 2**n in little-endian order,
225+
meaning the most significant digit is last. The most
226+
significant digit is guaranteed to be non-zero.
227+
If a is 0 then the output is an empty list.
228+
229+
"""
218230
a_digits = [0] * ((a.bit_length() + n - 1) // n)
219231

220232
def inner(x, L, R):
@@ -234,7 +246,9 @@ def inner(x, L, R):
234246

235247

236248
def _digits2int(digits, n):
237-
"""combine base-2**n digits into an int"""
249+
"""Combine base-2**n digits into an int. This function is the
250+
inverse of `_int2digits`. For more details, see _int2digits.
251+
"""
238252

239253
def inner(L, R):
240254
if L + 1 == R:
@@ -264,7 +278,9 @@ def _divmod_pos(a, b):
264278

265279

266280
def int_divmod(a, b):
267-
"""Asymptotically fast replacement for divmod, for 'int'."""
281+
"""Asymptotically fast replacement for divmod, for 'int'.
282+
Its time complexity is O(n**1.58), where n = #bits(a) + #bits(b).
283+
"""
268284
if _DEBUG:
269285
print('int_divmod', a.bit_length(), b.bit_length(), file=sys.stderr)
270286
if b == 0:
@@ -275,7 +291,5 @@ def int_divmod(a, b):
275291
elif a < 0:
276292
q, r = int_divmod(~a, b)
277293
return ~q, b + ~r
278-
elif a == 0:
279-
return 0, 0
280294
else:
281295
return _divmod_pos(a, b)

0 commit comments

Comments
 (0)