@@ -164,8 +164,9 @@ def str_to_int(s):
164
164
165
165
166
166
# 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".
169
170
170
171
_DIV_LIMIT = 1000
171
172
@@ -214,7 +215,18 @@ def _div3n2n(a12, a3, b, b1, b2, n):
214
215
215
216
216
217
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
+ """
218
230
a_digits = [0 ] * ((a .bit_length () + n - 1 ) // n )
219
231
220
232
def inner (x , L , R ):
@@ -234,7 +246,9 @@ def inner(x, L, R):
234
246
235
247
236
248
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
+ """
238
252
239
253
def inner (L , R ):
240
254
if L + 1 == R :
@@ -264,7 +278,9 @@ def _divmod_pos(a, b):
264
278
265
279
266
280
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
+ """
268
284
if _DEBUG :
269
285
print ('int_divmod' , a .bit_length (), b .bit_length (), file = sys .stderr )
270
286
if b == 0 :
@@ -275,7 +291,5 @@ def int_divmod(a, b):
275
291
elif a < 0 :
276
292
q , r = int_divmod (~ a , b )
277
293
return ~ q , b + ~ r
278
- elif a == 0 :
279
- return 0 , 0
280
294
else :
281
295
return _divmod_pos (a , b )
0 commit comments