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

2023-04-26 14:10 更新

Python math 模塊 math 模塊


描述

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


語法

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

math.ldexp(x, i)

參數(shù)說明:

  • x -- 必需,一個(gè)正數(shù)或負(fù)數(shù)。如果值不是數(shù)字,則返回 TypeError。
  • i -- 必需,一個(gè)正數(shù)或負(fù)數(shù)。如果值不是數(shù)字,則返回 TypeError。

返回值

一個(gè)浮點(diǎn)值,返回 x * (2**i)


實(shí)例

以下實(shí)例計(jì)算 x * (2**i):

# 導(dǎo)入 math 包
import math

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

輸出結(jié)果:

72.0
-20.0
60.0

Python math 模塊 math 模塊


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號