I want an explanation about the following expression(in description) which uses floor division, modulo, and comparison operators | SoloLearn: Learn to code for FREE!

+9

I want an explanation about the following expression(in description) which uses floor division, modulo, and comparison operators

print((-9//4)==(-(9//4+1)) and (-9%4)==(4-9%4)) What is the output? Also, help me to understand it better.

5/28/2020 1:24:57 PM

Eshan Gayanga

18 Answers

New Answer

+6

// is python floor division which always returns Quecent (for float value it returns smaller one. Example- >>9//4 2 And >>>-9/4=-2.25 >>>-9//4 -3(smaller than -2. 25) Python modulo operator always return the remainder having the same sign as the divisor. >>> -5 % 3 1 >>> 5 % -3 -1 >>> -5 % 3 = (1 -2*3) % 3 = 1 5 % -3 = (-1 * -2*-3) % 3 = -1 Similarly >>>-9%4=(3-4*3) %4=3 Hope it's clear now👍

+8

Eshan Gayanga Output is True Note :Floor division returns the quotient(answer or result of division) in which the digits after the decimal point are removed. But if one of the operands(dividend and divisor) is negative, then the result is floored, i.e., rounded away from zero(means, towards the negative of infinity). Let's separate the expressions 1.print(-9//4) 👈 -3 2.print(-(9//4+1)) 👈 -3 3.print(-9%4) 👈 3 4.print(4-9%4) 👈 3 To know the concept of floor division , we'll do our normal division so in 1st expression -9/4 will return -2.25 but -9//4 will return-3 bcoz 2.25 is rounded away from 0 & became -3 now in 2nd expression (-(9/4+1) = (-(2.25+1)=-3.25 but (-(9//4+1)=(-(2+1)=-3 in 3rd expression -9%4=3 If you do -9 // 4, you get -2.25 rounded down, which is -3. Multiply that by 4 and you get -12; 3 is the number that you'd have to add to that to get -9. Therefore, -9 % 4 is 3 in last expression (4-9%4)=4-1=3 🙂

+6

// is floor division -9//4 = -2.25 =-3 (-(9//4+1)) Here 9//4=2.25=2 So -(2+1)=-3 -3==-3 so here 2.25 is rounded to value smaller than it which is 2 because of floor division but in -2.25 it is rounded to -3 as -3 is smaller than -2.25 and sorry I don't have any good explanation for (-9%4) ,but you have already got an answer from others

+6

print((-9//4)==(-(9//4+1)) and (-9%4)==(4-9%4)) # -9//4 => -3 # -(9//4+1) => -(2+1) => -3 # -9%4 => 3 # 4-9%4 => 4-1 => 3 # print((-3 == -3) and (3 == 3)) => print(True and True) => print(True)

+6

Lay_in_life thanks sir👍

+5

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 you didn't explained how? As he can check the output by running it in code playground itself ,

+5

Why is the third expression 3? 🤔

+5

👍Clear now.🙂Thank you everyone!

+5

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 thks

+4

Please why -9//4=(-(9//4+1))

+4

Abhay wait I'm writing the explanation already

+4

I think his doubts was How (-9%4) =3 And no one is giving explanation about it,

+4

Ok.thanks sir.

+2

Eshan Gayanga Abhay HonFu Plz check my explanation for -9%4

+2

Lay_in_life 👏👏👏👏👏

+2

thanks again I will have a better way for a

+1

Eshan Gayanga Shobhit Pandey Abhay HonFu Sorry to say guys, my internet was slow, so I couldn't write my explanation , now it's 1024/1024 I have written each concept completely So if anyone has any doubt plz feel free to ask

0

Pada Oktober