在 NumPy 中返回給定值的尾數和指數對
要返回給定值的尾數和指數對,請在 Python NumPy 中使用 **numpy.frexp()** 方法。out 是結果儲存到的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果未提供或為 None,則返回一個新分配的陣列。元組(只能作為關鍵字引數)的長度必須等於輸出的數量。
條件在輸入上廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立了一個未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化狀態。
步驟
首先,匯入所需的庫 -
import numpy as np
要返回給定值的尾數和指數對,請在 Python NumPy 中使用 numpy.frexp() 方法。
檢查浮點數 -
print("Result? ", np.frexp(6.9)) print("
Result? ", np.frexp(-4.8))
檢查整數和無窮大 -
print("
Result? ", np.frexp(40)) print("
Result? ", np.frexp(-np.inf))
檢查 NaN 和無窮大 -
print("
Result? ", np.frexp(np.nan)) print("
Result? ", np.frexp(np.inf))
檢查對數 -
print("
Result? ", np.frexp(np.log(1))) print("
Result? ", np.frexp(np.log(2)))
示例
import numpy as np # To return mantissa and exponent as a pair of a given value, use the numpy.frexp() method in Python Numpy print("Returning the mantissa and exponent...
") # Check for float print("Result? ", np.frexp(6.9)) print("
Result? ", np.frexp(-4.8)) # Check for int and inf print("
Result? ", np.frexp(40)) print("
Result? ", np.frexp(-np.inf)) # Check for nan and inf print("
Result? ", np.frexp(np.nan)) print("
Result? ", np.frexp(np.inf)) # Check for log print("
Result? ", np.frexp(np.log(1))) print("
Result? ", np.frexp(np.log(2)))
輸出
Returning the mantissa and exponent... Result? (0.8625, 3) Result? (-0.6, 3) Result? (0.625, 6) Result? (-inf, 0) Result? (nan, 0) Result? (inf, 0) Result? (0.0, 0) Result? (0.6931471805599453, 0)
廣告