Why is result -3? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

27th Jan 2019, 5:02 PM
Gabriel Felix dos Santos
Gabriel Felix dos Santos - avatar
7 Answers
+ 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.
27th Jan 2019, 6:54 PM
Diego
Diego - avatar
+ 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
27th Jan 2019, 7:00 PM
Tibor Santa
Tibor Santa - avatar
+ 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
27th Jan 2019, 7:12 PM
Gabriel Felix dos Santos
Gabriel Felix dos Santos - avatar
+ 1
But 5//2 == 2. I am completely confused now.
27th Jan 2019, 6:56 PM
Maneren
Maneren - avatar
+ 1
Maneren 5/2 == 2.5 The greatest integer less than or equal to 2.5 is 2.
27th Jan 2019, 7:02 PM
Diego
Diego - avatar
+ 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.**
27th Jan 2019, 7:05 PM
Diego
Diego - avatar
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.
27th Jan 2019, 7:02 PM
Gabriel Felix dos Santos
Gabriel Felix dos Santos - avatar