Why -4%1.5 in python shows 0.5?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why -4%1.5 in python shows 0.5??

Please explain.........

11th Feb 2020, 4:50 PM
Sajal Chuttani
Sajal Chuttani - avatar
3 Answers
+ 1
-4 + 1.5 * 3 = 0.5
11th Feb 2020, 5:01 PM
Seb TheS
Seb TheS - avatar
+ 4
Sajal Chuttani In -4 % 1.5 The result: -Must be any number, which is 1.5 * N steps away from -4. (N can be any integer) -Must be more or equal to 0 -Must be less than 1.5 (There can only be 1 result.) In this case N must be 3, because -4 + 1.5 * 3 = 0.5 and 0 <= 0.5 < 1.5. But if: n = 2: -4 + 1.5 * 2 = -1 ¬(0 <= -1 < 1.5) n = 4 -4 + 1.5 * 4 = 2 ¬(0 <= 2 < 1.5) Mathematically: x % y gives: x + n * y for the only n integer value, for which condition: 0 <= x + n * y < y would be true.
11th Feb 2020, 5:24 PM
Seb TheS
Seb TheS - avatar
0
Hey! I guess i dont understand the modulus completely i was told it just returns the remainder but what is being done here.......??
11th Feb 2020, 5:03 PM
Sajal Chuttani
Sajal Chuttani - avatar