Differences On Numpyp.Floor And Python Int method.md

Posted by yaohong on Thursday, December 31, 2020

TOC

Differences On Numpyp.Floor And Python Int method.md

numpy.floor Return the floor of the input.

The floor of the scalar x is the largest integer i, such that i <= x;

np.floor() will not change its data type;

Example:

import numpy as np

tmp_list = list([-3.3, -2.22, -1.56, -0.56, 0.56, 1.56, 2.22, 3.33])

tmp_list = np.array(tmp_list)
print("type(tmp_list[0]):", type(tmp_list[0]) )
print("type(np.floor(tmp_list)[0]):",  type(np.floor(tmp_list)[0]) )
print("np.floor(tmp_list):", np.floor(tmp_list))

# Output:
# type(tmp_list[0]): <class 'numpy.float64'>
# type(np.floor(tmp_list)[0]): <class 'numpy.float64'>
# np.floor(tmp_list): [-4. -3. -2. -1.  0.  1.  2.  3.]


for x in tmp_list:
	print("x:",x,",int:",int(x)) 

# output:
# x: -3.3 ,int: -3
# x: -2.22 ,int: -2
# x: -1.56 ,int: -1
# x: -0.56 ,int: 0
# x: 0.56 ,int: 0
# x: 1.56 ,int: 1
# x: 2.22 ,int: 2
# x: 3.33 ,int: 3

For floating point numbers, int() will truncates toward zero, so -0.1 will by truncate to 0, -1.8 to -1; And its data type will be changed to <class 'int'>.

「点个赞」

Yaohong

点个赞

使用微信扫描二维码完成支付