+6

How is -6//4 equal to -2?

Normally if i type print(6//4) the result is 1 But, >>>print (-6//4) gives me -2 Why and how does that happen?

12/13/2019 2:47:50 PM

Tausimman Minuha¯\_(ツ)_/¯

7 Answers

New Answer

+10

This question is frequently asked and also anwered. You can read this: https://stackoverflow.com/questions/37283786/floor-division-with-negative-number

+9

6/4 = 1.5 6//4 = 1 # floor division -6//4 = -2 # floor value of -1.5

+6

I think it could be about modulo operator. Because I assume that: a % b = a - (a // b) * b If we tested -6 % 4 with the formula: -6 % 4 = -6 - (-6 // 4) * 4 If -6 // 4 was -1: -6 - (-1) * 4 = -2 Then we would get -2 which is the wrong remainder. If we always decided to take the lower whole number in case the result would not be a whole number. Then we would assume that -6 // 4 = -2 and calculate the remainder again: -6 - (-2) * 4 = 2 And this is the right remainder. I think this would be good reason to make floor division always choose the lower whole number in case of not getting a whole number.

+5

The floor is the largest integer less than or equal to the fractional result.

+2

Since -1 is greater than -2 hence python prints -2 as python rounds the answer off to the nearest and smallest digit

+1

Basically // rounds down to the nearest integer. 6//4=1.5 ~ 1(not 2), as 1<2 -6//4=-1.5 ~ -2(not -1), as -2<-1