Skip to content

Instantly share code, notes, and snippets.

@nkabrown
Last active May 12, 2016 15:21
Show Gist options
  • Save nkabrown/f8adb91b638d6502ce18f2a33a42d504 to your computer and use it in GitHub Desktop.
Save nkabrown/f8adb91b638d6502ce18f2a33a42d504 to your computer and use it in GitHub Desktop.
short discussion of modulo

Modulo

Wolfram defines the modulo operation as "m mod n is the remainder on division of m by n. The sign of m mod n for real m, n is always the same sign of n."

Python has a true modulo operator.

-5 % 2
# => 1

5 % -2
# => -1

JavaScript does not have a modulo operator. According to the ECMAScript Specification "The % MultiplicativeOperator yields the remainder of its operands from an implied division; the left operand is the dividend and the right operand is the divisor."

Most importantly "The sign of the result equals the sign of the dividend."

JavaScript has a remainder operator, not a modulo operator.

-5 % 2
// => -1

5 % -2
// => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment