TUTORIAL编程语言与算法FETCHED

Python math.log2() 方法

Python math.log2() 方法 Python math 模块 Python math.log2(x) 方法返回 x 以 2 为底的对数。 语法 math.log2() 方法语法如下: math.log2(x) 参数说明: x -- 必需,数字。如果 x 不是一个数字,返回 TypeError。如果值为 0 或负数,则返回 ValueError。 返回值 返回一个整数浮点数 float,表示一个数字以 ..

更新于 2026/7/16

教程详情页默认 noindex。

Python math.log2() 方法

Python math 模块 Python math 模块

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

语法

math.log2() 方法语法如下:

math.log2(x)

参数说明:

  • x -- 必需,数字。如果 x 不是一个数字,返回 TypeError。如果值为 0 或负数,则返回 ValueError。

返回值

返回一个整数浮点数 float,表示一个数字以 2 为底的自然对数。

实例

以下实例返回一个数字以 2 为底的自然对数:

实例

# 导入 math 包
import math

# 输出一个数字以 2 为底的自然对数
print(math.log2(2.7183))
print(math.log2(2))
print(math.log2(1))

输出结果:

1.4427046851812222
1.0
0.0

Python math 模块 Python math 模块