The Documentation about the floor division in Python says,
The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. 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.
I'm focusing on the last line, that says,
the result is that of mathematical division with the ‘floor’ function applied to the result
I get a float
in:
>>> 10//2.0
5.0
But an integer
in:
>>> import math
>>> math.floor(10/2.0)
5
So it's clearly not the math.floor
function that Python floor division is using. I want to know what exact procedure is Python following to compute the floor division of two arguments.
Because when taking the floor division of complex
numbers, Python says,
>>> (2+3j)//(4+5j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't take floor of complex number.
That means Python takes the division and then applies some sort of floor function. What really is it?
Compare (both from that page):
If they meant
math.floor
, they would've styled it like they styledfmod
, i.e., as code and withmath.
prefix and with parentheses and with link. What they do mean there is just the mathematical floor function. Not a Python function.Digging into how CPython implements it, let's first disassemble:
Looking up
BINARY_FLOOR_DIVIDE
:Looking up
PyNumber_FloorDivide
Looking up
nb_floor_divide
:Now
int
andfloor
both have their own:Looking up the function for the
float
one:Looking up
float_floor_div
:Looking up
_float_div_mod
:And I think
floor
is from C, so depends on whatever C compiler was used to compile your CPython.