In javascript the modulo operation result is like this:
-9 % 19 = -9
while in python the same modulo operation yields this:
-9 % 19 = 10
now according to Finite Field python's answer is correct. can anyone please explain why JavaScript doesn't return result same as python?
It is a matter of conventions (see here) and, for Python it is related to the result of integer divisions. Python works on the basis that you can reverse the division and get back to the original numerator by multiplying the quotient by the denominator and adding the modulo:
So, if N divided by D gives a quotient of Q and remainder or R (N mod D), then Q x D + R = N
Applying the numbers (Python):
-9 / 19 --> Q = -1, R = 10 --> -1 x 19 + 10 = -9
this also means that 9 % -19 --> -10
9 / -19 --> Q = -1, R = -10 --> -1 x -19 + -10 = 9
Javascript uses a different approach for modulo of negative numbers (applying the sign of the numerator to the remainder of the division between absolute values) that does not reverse correctly when applying the mathematical definition to numerator and denominator that have different signs:
Math.floor(-9/19)) --> -1
-9 % 19 --> -9
--> -1 x 19 + -9 --> -28
Math.floor(9/-19)) --> -1
9 % -19 --> 9
--> -1 x -19 + 9 --> 28