I did find the answer in another StackOverflow question and answer JavaScript % (modulo) gives a negative result for negative numbers and I wanted to share it for people who like me only looked for a "CoffeeScript" related explanations and thus have a hard time finding the correct answer.
The reason for using a %% b
which compiles to (a % b + b) % b
is that for negative number, like -5 % 3
, JavaScript will produce a negative number -5 % 3 = -2
while the correct mathematical answer should be -5 % 3 = 1
.
The accepted answer refers to an article on the JavaScript modulo bug which explains it well.