在 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

更新于: 2022年2月8日

186 次查看

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.