+ 9

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?

13th Dec 2019, 2:47 PM
Tausimman Minuha¯\_(ツ)_/¯
Tausimman Minuha¯\_(ツ)_/¯ - avatar
10 Antworten
+ 11
This question is frequently asked and also anwered. You can read this: https://stackoverflow.com/questions/37283786/floor-division-with-negative-number
13th Dec 2019, 2:54 PM
Lothar
Lothar - avatar
+ 7
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.
13th Dec 2019, 4:29 PM
Seb TheS
Seb TheS - avatar
+ 7
6/4 = 1.5 6//4 = 1 # floor division -6//4 = -2 # floor value of -1.5
14th Dec 2019, 2:03 AM
Sreeraj V A
Sreeraj V A - avatar
+ 5
The floor is the largest integer less than or equal to the fractional result.
14th Dec 2019, 10:32 PM
Sonic
Sonic - avatar
+ 3
Since -1 is greater than -2 hence python prints -2 as python rounds the answer off to the nearest small digit
15th Dec 2019, 11:25 AM
Raunak Yadav
+ 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
15th Dec 2019, 5:19 AM
LilBluey
LilBluey - avatar
+ 1
Its floor division. Round down the decimal result to next lower whole number.
6th Jan 2022, 11:18 AM
Sreeraj V A
Sreeraj V A - avatar
0
Floor function = largest number less than the number. -6/4 = -1.5 the floor function to -1.5 == -2 not -1 because -2<-1.5<-2. ..
14th Sep 2020, 9:30 AM
Mohammed Marzuq Rahman
- 3
print(6//1.5)
6th Jan 2022, 9:32 AM
Андрій Бондаренко
Андрій Бондаренко - avatar