Replace _nbits() with int.bit_length().

This commit is contained in:
Raymond Hettinger 2010-11-27 08:09:40 +00:00
parent 76338ec37d
commit db213a297d
1 changed files with 1 additions and 17 deletions

View File

@ -5526,23 +5526,7 @@ def _normalize(op1, op2, prec = 0):
##### Integer arithmetic functions used by ln, log10, exp and __pow__ #####
# This function from Tim Peters was taken from here:
# http://mail.python.org/pipermail/python-list/1999-July/007758.html
# The correction being in the function definition is for speed, and
# the whole function is not resolved with math.log because of avoiding
# the use of floats.
def _nbits(n, correction = {
'0': 4, '1': 3, '2': 2, '3': 2,
'4': 1, '5': 1, '6': 1, '7': 1,
'8': 0, '9': 0, 'a': 0, 'b': 0,
'c': 0, 'd': 0, 'e': 0, 'f': 0}):
"""Number of bits in binary representation of the positive integer n,
or 0 if n == 0.
"""
if n < 0:
raise ValueError("The argument to _nbits should be nonnegative.")
hex_n = "%x" % n
return 4*len(hex_n) - correction[hex_n[0]]
_nbits = int.bit_length
def _sqrt_nearest(n, a):
"""Closest integer to the square root of the positive integer n. a is