+ 3

In python (-11//3)=-4 why??

Python

27th Nov 2019, 7:58 AM
Abanikanta Biswal
Abanikanta Biswal - avatar
4 Antworten
+ 6
-4 is the largest integer less than or equal to -3.6666666666666....
27th Nov 2019, 9:16 AM
Sonic
Sonic - avatar
+ 4
11/3 = 3.66666 11//3 = 3 (floor value is 3) -11//3 = -4 (floor value of -3.6 is -4)
27th Nov 2019, 8:38 AM
Sreeraj V A
Sreeraj V A - avatar
+ 1
Yeah got it tq all
27th Nov 2019, 9:05 AM
Abanikanta Biswal
Abanikanta Biswal - avatar
+ 1
I think thats because // and % have something common. If we think that a % b = a - (a//b)*b And 0 <= a % b < b, when b > 0 (0 >= a % b > b, when b < 0) If for example -8 // 3 = -2, then -8 % 3 = -8 - (-2)*3 = -2 and -2 < 0 which should not happen because -8 % 3 = 1 This problem would appear almost always when a <= 0 and b > 0 (exception case when a % b = 0). But if -8 // 3 = -3, then -8 % 3 = -8 - (-3)*3 = 1 and it would be correct.
27th Nov 2019, 9:39 AM
Seb TheS
Seb TheS - avatar