在 python 中登录到 base 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3719631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:24:22  来源:igfitidea点击:

Log to the base 2 in python

pythonlogarithm

提问by Compuser7

How should I compute log to the base two in python. Eg. I have this equation where I am using log base 2

我应该如何在 python 中计算以 2 为基数的日志。例如。我有这个方程,我使用的是对数基数 2

import math
e = -(t/T)* math.log((t/T)[, 2])

采纳答案by unutbu

It's good to know that

很高兴知道

alt text

替代文字

but also know that math.logtakes an optional second argument which allows you to specify the base:

但也知道它 math.log需要一个可选的第二个参数,它允许您指定基数:

In [22]: import math

In [23]: math.log?
Type:       builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form:    <built-in function log>
Namespace:  Interactive
Docstring:
    log(x[, base]) -> the logarithm of x to the given base.
    If the base not specified, returns the natural logarithm (base e) of x.


In [25]: math.log(8,2)
Out[25]: 3.0

回答by Alexandre C.

log_base_2(x) = log(x) / log(2)

log_base_2(x) = log(x) / log(2)

回答by Conor

logbase2(x) = log(x)/log(2)

logbase2(x) = log(x)/log(2)

回答by puzz

>>> def log2( x ):
...     return math.log( x ) / math.log( 2 )
... 
>>> log2( 2 )
1.0
>>> log2( 4 )
2.0
>>> log2( 8 )
3.0
>>> log2( 2.4 )
1.2630344058337937
>>> 

回答by Platinum Azure

Don't forget that log[base A] x = log[base B] x / log[base B] A.

不要忘记log[base A] x = log[base B] x / log[base B] A

So if you only have log(for natural log) and log10(for base-10 log), you can use

因此,如果您只有log(对于自然对数)和log10(对于基数为 10 的对数),则可以使用

myLog2Answer = log10(myInput) / log10(2)

回答by log0

http://en.wikipedia.org/wiki/Binary_logarithm

http://en.wikipedia.org/wiki/Binary_logarithm

def lg(x, tol=1e-13):
  res = 0.0

  # Integer part
  while x<1:
    res -= 1
    x *= 2
  while x>=2:
    res += 1
    x /= 2

  # Fractional part
  fp = 1.0
  while fp>=tol:
    fp /= 2
    x *= x
    if x >= 2:
        x /= 2
        res += fp

  return res

回答by riza

Using numpy:

使用 numpy:

In [1]: import numpy as np

In [2]: np.log2?
Type:           function
Base Class:     <type 'function'>
String Form:    <function log2 at 0x03049030>
Namespace:      Interactive
File:           c:\python26\lib\site-packages\numpy\lib\ufunclike.py
Definition:     np.log2(x, y=None)
Docstring:
    Return the base 2 logarithm of the input array, element-wise.

Parameters
----------
x : array_like
  Input array.
y : array_like
  Optional output array with the same shape as `x`.

Returns
-------
y : ndarray
  The logarithm to the base 2 of `x` element-wise.
  NaNs are returned where `x` is negative.

See Also
--------
log, log1p, log10

Examples
--------
>>> np.log2([-1, 2, 4])
array([ NaN,   1.,   2.])

In [3]: np.log2(8)
Out[3]: 3.0

回答by akashchandrakar

If you are on python 3.4 or above then it already has a built-in function for computing log2(x)

如果你使用的是 python 3.4 或更高版本,那么它已经有一个用于计算 log2(x) 的内置函数

import math
'finds log base2 of x'
answer = math.log2(x)

If you are on older version of python then you can do like this

如果您使用的是旧版本的python,那么您可以这样做

import math
'finds log base2 of x'
answer = math.log(x)/math.log(2)

回答by Bob Stein

float → float math.log2(x)

浮动 → 浮动 math.log2(x)

import math

log2 = math.log(x, 2.0)
log2 = math.log2(x)   # python 3.4 or later


float → int math.frexp(x)

浮动 → 整数 math.frexp(x)

If all you need is the integer part of log base 2 of a floating point number, extracting the exponent is pretty efficient:

如果您只需要浮点数的对数基数 2 的整数部分,那么提取指数非常有效:

log2int_slow = int(math.floor(math.log(x, 2.0)))
log2int_fast = math.frexp(x)[1] - 1
  • Python frexp() calls the C function frexp()which just grabs and tweaks the exponent.

  • Python frexp() returns a tuple (mantissa, exponent). So [1]gets the exponent part.

  • For integral powers of 2 the exponent is one more than you might expect. For example 32 is stored as 0.5x2?. This explains the - 1above. Also works for 1/32 which is stored as 0.5x2??.

  • Floors toward negative infinity, so log?31 is 4 not 5. log?(1/17) is -5 not -4.

  • Python frexp() 调用C 函数 frexp(),它只是抓取和调整指数。

  • Python frexp() 返回一个元组(尾数,指数)。所以[1]得到指数部分。

  • 对于 2 的整数幂,指数比您预期的多 1。例如 32 存储为 0.5x2?。这解释了- 1上面的内容。也适用于存储为 0.5x2?? 的 1/32。

  • 负无穷大的楼层,所以 log?31 是 4 而不是 5。 log?(1/17) 是 -5 而不是 -4。



int → int x.bit_length()

整数 → 整数 x.bit_length()

If both input and output are integers, this native integer method could be very efficient:

如果输入和输出都是整数,这个原生整数方法可能非常有效:

log2int_faster = x.bit_length() - 1
  • - 1because 2? requires n+1 bits. Works for very large integers, e.g. 2**10000.

  • Floors toward negative infinity, so log?31 is 4 not 5. log?(1/17) is -5 not -4.

  • - 1因为 2?需要 n+1 位。适用于非常大的整数,例如2**10000.

  • 负无穷大的楼层,所以 log?31 是 4 而不是 5。 log?(1/17) 是 -5 而不是 -4。

回答by Akash Kandpal

Try this ,

尝试这个 ,

import math
print(math.log(8,2))  # math.log(number,base)