0
Why -4%1.5 in python shows 0.5??
Please explain.........
4 Respuestas
+ 1
-4 + 1.5 * 3 = 0.5
+ 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.
0
Hey! I guess i dont understand the modulus completely i was told it just returns the remainder but what is being done here.......??