Python math.log2() 方法

定义和用法

math.log2() 方法返回数的以 2 为底的对数。

实例

求不同数的以 2 为底的对数:

# 导入 math 库
import math 

# 返回不同数的以 2 为底的对数:
print(math.log2(2.7183))
print(math.log2(2))
print(math.log2(1))

亲自试一试

语法

math.log2(x)

参数

参数 描述
x

必需。指定要计算对数的值。

如果该值为 0 或负数,它将返回 ValueError。

如果该值不是数,它将返回 TypeError。

技术细节

返回值: 浮点数,表示数的以 2 为底的对数。
Python 版本: 3.3