What is the difference between int() and floor() in Python 3? What is the difference between int() and floor() in Python 3? python-3.x python-3.x

What is the difference between int() and floor() in Python 3?


floor() rounds down. int() truncates. The difference is clear when you use negative numbers:

>>> import math>>> math.floor(-3.5)-4>>> int(-3.5)-3

Rounding down on negative numbers means that they move away from 0, truncating moves them closer to 0.

Putting it differently, the floor() is always going to be lower or equal to the original. int() is going to be closer to zero or equal.


I test time complexity of both method they are the same

from time import timeimport mathimport randomr = 10000000def 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))

output is:

# function floor takes 11.841985# function int   takes 11.841325