返回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