# 7 examples of 'python log2' in Python

Every line of 'python log2' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``15def log2(x):16    try:17        return math.log(x, 2)18    except ValueError:19        return float("nan")``
``14def log2 (x):15    return math.log (x, 2)``
``65def log2(x):66    """ Take log base 2 of x.67    """68    x = _fix_real_lt_zero(x)69    return nx.log(x)/_ln2``
``34def log2(val):35    """36    Calculate the log base 2 of a value.37    """38    return int(math.log(val, 2))``
``340def log2(x):341    """342    Compute the logarithm base 2 of `x`.343344    Return the "principal value" (for a description of this, see345    `numpy.log2`) of :math:`log_2(x)`. For real `x > 0`, this is346    a real number (``log2(0)`` returns ``-inf`` and ``log2(np.inf)`` returns347    ``inf``). Otherwise, the complex principle value is returned.348349    Parameters350    ----------351    x : array_like352       The value(s) whose log base 2 is (are) required.353354    Returns355    -------356    out : ndarray or scalar357       The log base 2 of the `x` value(s). If `x` was a scalar, so is `out`,358       otherwise an array is returned.359360    See Also361    --------362    numpy.log2363364    Notes365    -----366    For a log2() that returns ``NAN`` when real `x < 0`, use `numpy.log2`367    (note, however, that otherwise `numpy.log2` and this `log2` are368    identical, i.e., both return ``-inf`` for `x = 0`, ``inf`` for `x = inf`,369    and, notably, the complex principle value if ``x.imag != 0``).370371    Examples372    --------373    We set the printing precision so the example can be auto-tested:374375    >>> np.set_printoptions(precision=4)376377    >>> np.emath.log2(8)378    3.0379    >>> np.emath.log2([-4, -8, 8])380    array([ 2.+4.5324j,  3.+4.5324j,  3.+0.j    ])381382    """383    x = _fix_real_lt_zero(x)384    return nx.log2(x)``
``9def log2(x):10  return math.log(x)/math.log(2.)``
``112def exact_log2(number):113  """Find and return an unsigned integer i >= 0 such that ``number == 2**i``. If114  no such integer exists, this function raises ValueError.115116  .. NOTE:117      It essentially answers this question:118119      "How many times would you have to multiply 2 into itself to120      get the given number?"121122  Taken from PyCrypto.123124  :param number:125      Unsigned integer.126  :returns:127      An integer i >= 0 such that number == 2**i.128  """129  num = number130  if num <= 0:131    raise ValueError("Cannot compute logarithm of non-positive integer")132  i = 0133  while num:134    if (num & 1) and num != 1:135      raise ValueError("No solution could be found")136    i += 1137    num >>= 1138  i -= 1139  #assert number == (1 << i)140  return i``