Search code examples
pythonpython-3.xfloor

How does floor division work in python?


  1. 5//2 = 2;
  2. 5//7 = 0;
  3. 5//-6 = -1;
  4. 5//-2 = -3;
  5. 5//-3 = -2;
  6. 5/-4 = -2;

Can someone explain the logic behind this?


Solution

  • The way it should:

    5 / 2 = 2.5        (2)
    5 / 7 = 0.714285   (0)
    5 / -6 = −0.8333   (-1 is the integer below -0.833333)
    5 / -2 = −2.5      (-3)
    5 / -3 = −1.6666   (-2)
    

    It's a basic floor. It divides it, and then makes it the integer below.