返回 NumPy 中值的整數和小數部分
要返回值的整數和小數部分,請在 Python NumPy 中使用 **numpy.modf()** 方法。如果給定數字為負數,則整數和小數部分也為負數。
out 是結果儲存到的位置。如果提供,則其形狀必須是輸入廣播到的形狀。如果未提供或為 None,則返回一個新分配的陣列。元組(僅作為關鍵字引數可能)的長度必須等於輸出的數量。
條件在輸入上廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立了一個未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化狀態。
步驟
首先,匯入所需的庫 -
import numpy as np
要返回值的整數和小數部分,請在 Python NumPy 中使用 numpy.modf() 方法 -
print("Returning the fractional and integral parts...
")
檢查浮點數 -
print("Result? ", np.modf(0.1))
print("
Result? ", np.modf(-0.7))檢查整數和無窮大 -
print("
Result? ", np.modf(5))
print("
Result? ", np.modf(-np.inf))檢查 NaN 和無窮大 -
print("
Result? ", np.modf(np.nan))
print("
Result? ", np.modf(np.inf))檢查對數 -
print("
Result? ", np.modf(np.log(1)))
print("
Result? ", np.modf(np.log(2)))示例
import numpy as np
# To return the fractional and integral parts of a value, use the numpy.modf() method in Python Numpy
print("Returning the fractional and integral parts...
")
# Check for float
print("Result? ", np.modf(0.1))
print("
Result? ", np.modf(-0.7))
# Check for int and inf
print("
Result? ", np.modf(5))
print("
Result? ", np.modf(-np.inf))
# Check for nan and inf
print("
Result? ", np.modf(np.nan))
print("
Result? ", np.modf(np.inf))
# Check for log
print("
Result? ", np.modf(np.log(1)))
print("
Result? ", np.modf(np.log(2)))輸出
Returning the fractional and integral parts... Result? (0.1, 0.0) Result? (-0.7, -0.0) Result? (0.0, 5.0) Result? (-0.0, -inf) Result? (nan, nan) Result? (0.0, inf) Result? (0.0, 0.0) Result? (0.6931471805599453, 0.0)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP