96

In Python 2, floor() returned a float value. Although not obvious to me, I found a few explanations clarifying why it may be useful to have floor() return float (for cases like float('inf') and float('nan')).

However, in Python 3, floor() returns integer (and returns overflow error for the special cases mentioned before).

So what is the difference, if any, between int() and floor() now?

vaultah
  • 44,105
  • 12
  • 114
  • 143
datah4ck3r
  • 1,174
  • 1
  • 8
  • 19
  • 2
    in short, `floor()` is the [mathematical floor `⌊⌋` function](https://en.wikipedia.org/wiki/Floor_and_ceiling_functions) and `int()` is equivalent to `sgn(x)⌊|x|⌋` in math – phuclv Nov 07 '20 at 09:34
  • By `floor`, probably `math.floor` is meant. – root Dec 25 '21 at 00:47

4 Answers4

225

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
17

I tested the time complexity of both methods, they are the same.

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))

Output is:

# function floor takes 11.841985
# function int   takes 11.841325
Community
  • 1
  • 1
amin saffar
  • 1,953
  • 3
  • 22
  • 34
3

Rounding down can be done in many ways, some methods are equivalent, e.g. built-in int, numpy.trunc and numpy.fix which truncate the number, meaning for a negative number they return a result which is larger than the argument. floor is different and actually returns the next smaller integer regardless of the sign. This can be confusing. Here is a summary of built-in and numpy operators:

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
         ROUNDING -2.8 -2.5 -2.2  2.2  2.5  2.8
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
            floor   -3   -3   -3    2    2    2
             rint   -3   -2   -2    2    2    3
        round (0)   -3   -2   -2    2    2    3
              fix   -2   -2   -2    2    2    2
            (int)   -2   -2   -2    2    2    2
            trunc   -2   -2   -2    2    2    2
             ceil   -2   -2   -2    3    3    3
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
mins
  • 6,478
  • 12
  • 56
  • 75
  • 1
    Interesting post but it would be much clearer if the columns with integers were removed as it is quite intuitive that all functions treat them equally. – Gianluca Micchi Jan 26 '23 at 10:24
  • 1
    @GianlucaMicchi: Right, thanks. Updating. This table was actually one of a set where integer values are not processed the same way by other functions (e.g. `divmod` remainder vs `fmod` when the divisor is negative). – mins Jan 26 '23 at 12:24
-1

I wrote this method, does the trick for me,

def rightInteger(numa, numb):
    result = numa/numb
    next_int = math.ceil(result)
    difference = next_int -result
    if(difference <=0.5):
        return next_int
    else:
        return math.floor(result)
Shamsul Arefin
  • 1,771
  • 1
  • 21
  • 21