Search code examples
pythoncastingfloor-division

Difference between "a//b" and "int(a/b)"


I know a/b is floating point division and a//b is floor division in Python.
It's seen that int(a/b) also results the same as floor division if both numerators and denominators are positive number. But on trying -a//b and int(-a/b) yield different results. What are the internal operations?

>>> int(-5/3)
-1
>>> -5//3
-2

How different is int(a/b) from the equivalent floor division i.e, a//b?


Solution

  • From int docs:

    For floating point numbers, this truncates towards zero.

    From // docs:

    Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result.