返回NumPy中輸入的截斷值
要返回輸入的截斷值,請在 Python NumPy 中使用 **numpy.trunc()** 方法。該函式返回 x 中每個元素的截斷值。如果 x 是標量,則這是一個標量。標量 x 的截斷值是距零比 x 更近的最近整數 i。簡而言之,帶符號數 x 的小數部分被丟棄。
此條件在輸入上進行廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化狀態。
步驟
首先,匯入所需的庫:
import numpy as np
要返回輸入的截斷值,請在 Python NumPy 中使用 numpy.trunc() 方法。檢查浮點數的 trunc():
print("Result? ", np.trunc(55.8))
print("
Result? ", np.trunc(-599.2))檢查無窮大的 trunc():
print("
Result? ", np.trunc(-np.inf))檢查 NaN 和無窮大的 trunc():
print("
Result? ", np.trunc(np.nan))
print("
Result? ", np.trunc(np.inf))檢查對數的 trunc():
print("
Result? ", np.trunc(np.log(1)))
print("
Result? ", np.trunc(np.log(2)))示例
import numpy as np
# To return the truncated value of the input, use the numpy.trunc() method in Python Numpy
print("Returning the trunc value...
")
# Check trunc() for float
print("Result? ", np.trunc(55.8))
print("
Result? ", np.trunc(-599.2))
# Check trunc() for inf
print("
Result? ", np.trunc(-np.inf))
# Check trunc() for nan and inf
print("
Result? ", np.trunc(np.nan))
print("
Result? ", np.trunc(np.inf))
# Check trunc() for log
print("
Result? ", np.trunc(np.log(1)))
print("
Result? ", np.trunc(np.log(2)))輸出
Returning the trunc value... Result? 55.0 Result? -599.0 Result? -inf Result? nan Result? inf Result? 0.0 Result? 0.0
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP