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