阅读(1190) (0)

Python3 math.ldexp() 方法 - 求x乘2的i次方

2023-03-15 16:43:55 更新

 math 模块 math 模块


描述

math.ldexp(x, i) 方法返回 x * (2**i),是math.frexp() 的反函数。


语法

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

math.ldexp(x, i)

参数说明:

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

返回值

一个浮点值,返回 x * (2**i)


实例

以下实例计算 x * (2**i):

# 导入 math 包
import math

# 返回 x * (2**i)
print(math.ldexp(9, 3))
print(math.ldexp(-5, 2))
print(math.ldexp(15, 2))

输出结果:

72.0
-20.0
60.0

Python math 模块 math 模块