ldexp()
函數是Python中的標準數學庫函數之一,該函數返回x *(2 ** i)。這也稱為Python的逆運算frexp()
函數。
用法: math.ldexp(x, i)
參數:
x:任何有效數字(+ ve或-ve)
i:任何有效數字(+ ve或-ve)
返回:返回x *(2 ** i)的值。
例如,如果x = 3和i = 4,則Math.ldexp(3,4)= 3 * 16 = 48。
代碼1:
# Python3 code demonstrate ldexp() function
# importing math library
import math
# ldexp() Function on +ve nd -ve Numbers
print(math.ldexp(9, 3))
print(math.ldexp(-5, 2))
# ldexp() Function on fractional Number
print(math.ldexp(3.5, 2))
print('%.2f' %math.ldexp(-6.8, 3))
輸出:
72.0 -20.0 14.0 -54.40
代碼2:
# Python3 code demonstrate ldexp() function
# importing math library
import math
# Tuple Declaration
tpl = (9, -5, 3.5, -6.8)
# List Declaration
lst = [13, 4, 8.4, -6.7]
# ldexp() Function on +ve nd -ve Numbers
print(math.ldexp(tpl[0], 3))
print(math.ldexp(tpl[3], 2))
# ldexp() Function on fractional Number
print(math.ldexp(lst[1], 2))
print('%.2f' %math.ldexp(lst[2], 3))
輸出:
72.0 -27.2 16.0 67.20
代碼3:如果X值或i值參數不是數字,則ldexp()函數將返回TypeError。
# Python3 code demonstrates when error occurs
# importing the math library
import math
# string value taken
print(math.ldexp('25', 5))
print(math.ldexp(25, '5'))
輸出:
TypeError:a float is required TypeError:a float is required
相關用法
- Python numpy.ldexp()用法及代碼示例
- Python int()用法及代碼示例
- Python oct()用法及代碼示例
- Python map()用法及代碼示例
- Python dir()用法及代碼示例
- Python id()用法及代碼示例
- Python sum()用法及代碼示例
- Python now()用法及代碼示例
- Python ord()用法及代碼示例
- Python cmp()用法及代碼示例
- Python hex()用法及代碼示例
- Python tell()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Python | ldexp() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。