0
What's the difference? -11//3 and 11//3
5 Réponses
+ 4
In programming there is a concept called flooring, which means rounding a decimal number to the smaller integer, which means that for example flooring value -4.1 would yield -5.0, which might surprise beginners who think that floor division is same than integer division.
In math module there is a function called floor:
from math import floor
which can be used to floor decimal numbers:
floor(9.81) -> 9
floor(4.00) -> 4
floor(0.99) -> 0 (...continuing soon... finished.)
floor(-2.55) -> -3
floor(-8.00) -> -8
Python's floor division is *about* equal to flooring normal division:
x//y == floor(x/y)
and why I say *about* equal, is because x//y and floor(x/y) don't always produce number of same type. x//y produces a float if either x or y is float while floor(x/y) always produces an integer.
+ 3
ceil points to positive infinity.
floor points to negative infinity.
That's why floor of a negative number becomes a bigger negative number.
ceil
^
positive num
v
floor
---------0--------- zero
ceil
^
negative num
v
floor
+ 2
This operator will divide the first argument by the second and round the result down to the nearest whole number,
-4 is lower than -3
+ 2
Thanks Shmuel Kroizer and Seb TheS . I learned something new today from you. thank you
+ 2
CYBER-B Are you saying that -11 // 3 yields -4, because -3.66666... is closer to -4 than -3? Then why -10 // 9 yields -2?