+ 1
Why is result -3?
I was on a chalenge and found this question: What is the output of this code? print(-5 // 2) My answer was -2, however, the correct was -3. I would like the reason of it's -3. The quocient would be -2, no? Cos 2 times -2 is equals -4. If someone knows it, please, would explain me? Here is the code and, really, the result is -3. https://code.sololearn.com/ck94LFY2mBka/?ref=app
7 Réponses
+ 5
Floor division (x//y) returns the greatest integer less than or equal to the division of x/y.
For example:
-5/2 == -2.5
The greatest integer less than or equal to -2.5 is -3.
+ 2
Floor: round down.
Ceil: round up.
For negative numbers the direction is still the same, floor(-2.5) takes the next integer that is less or equal, so it is - 3
+ 2
Diego Acero, yes, this question already was Answered. I'm Sorry, will search It on next time.
Here one link: https://www.sololearn.com/Discuss/1080582/?ref=app
+ 1
But 5//2 == 2. I am completely confused now.
+ 1
Maneren
5/2 == 2.5
The greatest integer less than or equal to 2.5 is 2.
+ 1
Also, I'm pretty sure this question has already been asked before, so next time please use the search bar before posting.
**Will be adding links when the page loads.**
0
Maneren, I got it. For example, the quocient operator return the greatest integer less than or equal the result. So, 5 // 2 = 2.5, flooring it, you'll get 2.
Already about -5 // 2, the result is -2.5, so, flooring it you'll get -3. It happens because -3 is less than -2.