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
this disclaimer
15def log2(x):
16 try:
17 return math.log(x, 2)
18 except ValueError:
19 return float("nan")
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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`.
343
344 Return the "principal value" (for a description of this, see
345 `numpy.log2`) of :math:`log_2(x)`. For real `x > 0`, this is
346 a real number (``log2(0)`` returns ``-inf`` and ``log2(np.inf)`` returns
347 ``inf``). Otherwise, the complex principle value is returned.
348
349 Parameters
350 ----------
351 x : array_like
352 The value(s) whose log base 2 is (are) required.
353
354 Returns
355 -------
356 out : ndarray or scalar
357 The log base 2 of the `x` value(s). If `x` was a scalar, so is `out`,
358 otherwise an array is returned.
359
360 See Also
361 --------
362 numpy.log2
363
364 Notes
365 -----
366 For a log2() that returns ``NAN`` when real `x < 0`, use `numpy.log2`
367 (note, however, that otherwise `numpy.log2` and this `log2` are
368 identical, i.e., both return ``-inf`` for `x = 0`, ``inf`` for `x = inf`,
369 and, notably, the complex principle value if ``x.imag != 0``).
370
371 Examples
372 --------
373 We set the printing precision so the example can be auto-tested:
374
375 >>> np.set_printoptions(precision=4)
376
377 >>> np.emath.log2(8)
378 3.0
379 >>> np.emath.log2([-4, -8, 8])
380 array([ 2.+4.5324j, 3.+4.5324j, 3.+0.j ])
381
382 """
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``. If
114 no such integer exists, this function raises ValueError.
115
116 .. NOTE:
117 It essentially answers this question:
118
119 "How many times would you have to multiply 2 into itself to
120 get the given number?"
121
122 Taken from PyCrypto.
123
124 :param number:
125 Unsigned integer.
126 :returns:
127 An integer i >= 0 such that number == 2**i.
128 """
129 num = number
130 if num <= 0:
131 raise ValueError("Cannot compute logarithm of non-positive integer")
132 i = 0
133 while num:
134 if (num & 1) and num != 1:
135 raise ValueError("No solution could be found")
136 i += 1
137 num >>= 1
138 i -= 1
139 #assert number == (1 << i)
140 return i

Related snippets