Processing math: 100%

返回NumPy中输入的向下取整值


要返回输入的向下取整值,请在Python NumPy中使用**numpy.floor()**方法。标量x的向下取整值是最大的整数i,使得i <= x。它通常表示为**X**。该函数返回x中每个元素的向下取整值。如果x是标量,则这是一个标量。

out是一个将结果存储其中的位置。如果提供,则其形状必须与输入广播到的形状相同。如果没有提供或为None,则返回一个新分配的数组。元组(只能作为关键字参数)的长度必须等于输出的数量。

步骤

首先,导入所需的库:

import numpy as np

要返回输入的向下取整值,请在Python NumPy中使用numpy.floor()方法。

检查浮点数的floor():

print("Result? ", np.floor(45.7))
print("
Result? ", np.floor(-487.4))

检查无穷大的floor():

print("
Result? ", np.floor(-np.inf))

检查NaN和无穷大的floor():

print("
Result? ", np.floor(np.nan)) print("
Result? ", np.floor(np.inf))

检查对数的floor():

print("
Result? ", np.floor(np.log(1))) print("
Result? ", np.floor(np.log(2)))

示例

Open Compiler
import numpy as np # To return the floor of the input, use the numpy.floor() method in Python Numpy print("Returning the floor value...") # Check floor() for float print("Result? ", np.floor(45.7)) print("Result? ", np.floor(-487.4)) # Check floor() for inf print("Result? ", np.floor(-np.inf)) # Check floor() for nan and inf print("Result? ", np.floor(np.nan)) print("Result? ", np.floor(np.inf)) # Check floor() for log print("Result? ", np.floor(np.log(1))) print("Result? ", np.floor(np.log(2)))

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

Returning the floor value...

Result? 45.0

Result? -488.0

Result? -inf

Result? nan

Result? inf

Result? 0.0

Result? 0.0

更新于:2022年2月8日

173 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告