在 NumPy 中返回零維陣列值之後朝另一個值方向的下一個浮點值
要返回一個值之後朝另一個值方向的下一個浮點值,按元素方式,請在 Python NumPy 中使用 **numpy.nextafter()** 方法。第一個引數是要查詢其下一個可表示值的數值。第二個引數是查詢下一個可表示值的尋找方向。
該函式返回 x1 在 x2 方向上的下一個可表示值。如果 x1 和 x2 都是標量,則為標量。
out 是儲存結果的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果未提供或為 None,則返回一個新分配的陣列。元組(僅作為關鍵字引數)的長度必須等於輸出的數量。
條件在輸入上進行廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立了一個未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化。
步驟
首先,匯入所需的庫 -
import numpy as np
使用 array() 方法建立兩個零維 NumPy 陣列 -
arr1 = np.array(0.1) arr2 = np.array(-np.inf)
顯示陣列 -
print("Array 1...
", arr1)
print("
Array 2...
", arr2)獲取陣列的型別 -
print("
Our Array 1 type...
", arr1.dtype)
print("
Our Array 2 type...
", arr2.dtype)獲取陣列的維度 -
print("
Our Array 1 Dimensions...
",arr1.ndim)
print("
Our Array 2 Dimensions...
",arr2.ndim)要返回一個值之後朝另一個值方向的下一個浮點值,按元素方式,請在 Python NumPy 中使用 numpy.nextafter() 方法。第一個引數是要查詢其下一個可表示值的數值。第二個引數是查詢下一個可表示值的尋找方向 -
print("
Result...
",np.nextafter(arr1, arr2))
示例
import numpy as np
# Creating two zero dimensional numpy array using the array() method
arr1 = np.array(0.1)
arr2 = np.array(-np.inf)
# Display the arrays
print("Array 1...
", arr1)
print("
Array 2...
", arr2)
# Get the type of the arrays
print("
Our Array 1 type...
", arr1.dtype)
print("
Our Array 2 type...
", arr2.dtype)
# Get the dimensions of the Arrays
print("
Our Array 1 Dimensions...
",arr1.ndim)
print("
Our Array 2 Dimensions...
",arr2.ndim)
# To return the next floating-point value after a value towards another value, element-wise., use the numpy.nextafter() method in Python Numpy
# The 1st parameter is the value to find the next representable value of.
# The 2nd parameter is the direction where to look for the next representable value.
print("
Result...
",np.nextafter(arr1, arr2))輸出
Array 1... 0.1 Array 2... -inf Our Array 1 type... float64 Our Array 2 type... float64 Our Array 1 Dimensions... 0 Our Array 2 Dimensions... 0 Result... 0.09999999999999999
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP