Python 3中的int()和floor()有什么区别?

dat*_*k3r 51 python floating-point python-3.x

在Python 2中,floor()返回了一个浮点值.虽然对我来说不是很明显,但我找到了一些解释,说明为什么floor()返回浮点数可能是有用的(对于像float('inf')和的情况float('nan')).

但是,在Python 3中,floor()返回整数(并返回前面提到的特殊情况的溢出错误).

那么现在int()floor()现在有什么不同呢?

Mar*_*ers 99

floor()几轮下来.int() 截断.使用负数时,差异很明显:

>>> import math
>>> math.floor(-3.5)
-4
>>> int(-3.5)
-3
Run Code Online (Sandbox Code Playgroud)

向下舍入负数表示它们从0移开,截断将它们移近0.

换句话说,它floor()总是低于或等于原始的.int()将接近零或相等.


ami*_*far 8

我测试了两种方法的时间复杂度它们是相同的

from time import time
import math
import random

r = 10000000
def floorTimeFunction():
  for i in range(r):
    math.floor(random.randint(-100,100))

def intTimeFunction():
  for i in range(r):
    int(random.randint(-100,100))

t0 = time()
floorTimeFunction()
t1 = time()
intTimeFunction()
t2 = time()

print('function floor takes %f' %(t1-t0))
print('function int   takes %f' %(t2-t1))
Run Code Online (Sandbox Code Playgroud)

输出是:

# function floor takes 11.841985
# function int   takes 11.841325
Run Code Online (Sandbox Code Playgroud)


min*_*ins 5

向下舍入可以通过多种方式完成,有些方法是等效的,例如built-in intnumpy.trunc并且numpy.fix截断数字,这意味着对于负数,它们返回大于参数的结果。floor不同,实际上返回下一个较小的整数,无论​​符号如何。这可能会令人困惑。以下是内置运算符和 numpy 运算符的摘要:

\n
\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\n         ROUNDING -2.8 -2.5 -2.2  2.2  2.5  2.8\n\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\xe2\x94\x88\n            floor   -3   -3   -3    2    2    2\n             rint   -3   -2   -2    2    2    3\n        round (0)   -3   -2   -2    2    2    3\n              fix   -2   -2   -2    2    2    2\n            (int)   -2   -2   -2    2    2    2\n            trunc   -2   -2   -2    2    2    2\n             ceil   -2   -2   -2    3    3    3\n\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\n
Run Code Online (Sandbox Code Playgroud)\n

  • @GianlucaMicchi:好的,谢谢。正在更新。该表实际上是其他函数以不同方式处理整数值的集合之一(例如,当除数为负数时,“divmod”余数与“fmod”)。 (2认同)