+ 3
Explain me why -4%3 output -1
8 Réponses
+ 5
% -->modulus operator(remainder)
4%3 => Remainder on dividing 4 by 3 which is 1 .
Therefore -4%3 = -1
+ 4
Alphin K Sajan is it a convention that the result take sign of first number or it can be demonstrated?
+ 4
Elon Yemp Yes,When we use modulus operator, the first value 's sign is considered, but not the second one's.
Eg:
-4 % 3= -1
-4 % -3= -1
But, 4 % -3 = 1
Hope now u understood !!
+ 3
John Robotane but with JavaScript (-4)%3 output -1
+ 3
John Robotane javascript output the same result for -(4%3)
-4%3 and
(-4%3)
+ 1
this may help:
(-4)%3=2
-4%3=-(4%3)=-(1)=-1
+ 1
Elon Yemp Wlcm :)
0
Alphin K Sajan yes i understand now! Thanks