1

According to Wikipedia, the modulo operator (remainder of integer division) on n should yield a result between 0 and n-1.

This is indeed the case in python:

print(-1%5) # outputs 4

In Ruby:

puts -1%5 # outputs 4

In Haskell

main = putStrLn $ show $ mod (-1) 5

But inJavascript:

console.log(-1%5);

The result is -1 !!

Why is that ? is there logic behind this design decision ?

While -1 is equivalent to 4 modulo 5, negative values makes it harder to use as indices.

For example:

arr[x%5]

would always be a valid expression in python and Ruby when the array length is more than 5, but in the Javascript, this is an exception waiting to happen.

6
  • You reference Wikipedia and yet it clearly states When either a or n is negative, the naive definition breaks down and programming languages differ in how these values are defined. So it's not like progamming languages are in breach of it as you also claim. Commented Nov 19, 2016 at 10:56
  • It is evident that this is the case, the question is why ? Commented Nov 19, 2016 at 10:57
  • Also I don't see how arr[x%5] is an exception waiting to happen in JS - if you pass in a negative number you'll get a negative result, so you'll get undefined. If the operator did indeed work like python or whatever, then it will give you a random element from the array. So you could have a bug and never find it until way later because it seems like it works. Commented Nov 19, 2016 at 10:58
  • As for "why" that's the case - the dupe you were linked to last time did have the answer - % is treated as "give me the remainder". That seems to be the rationale. Commented Nov 19, 2016 at 10:59
  • The end result is that the %n function has n possible values when applied in other languages (e.g. python,ruby,haskell) but in javascript it has 2n-1 possible values Commented Nov 19, 2016 at 11:26

2 Answers 2

3

If by "modulus" you understand the remainder of the Euclidean division as common in mathematics, the % operator is not the modulo operator. It rather is called the remainder operator, whose result always has the same sign as the dividend. (In Haskell, the rem function does this).

This behaviour is pretty common amongst programming languages, notably also in C and Java from which the arithmetic conventions of JavaScript were inspired.

Sign up to request clarification or add additional context in comments.

Comments

1

Note that while in most languages, ‘%’ is a remainder operator, in some (e.g. Python, Perl) it is a modulo operator. For two values of the same sign, the two are equivalent, but when the dividend and divisor are of different signs, they give different results. To obtain a modulo in JavaScript, in place of a % n, use ((a % n ) + n ) % n

see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder

1 Comment

that's not entirely true - negative divisor in python or perl always return a value <= 0, unlike the purist definition of modulo which requires >= 0 no matter what.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.