Modulo Arithmetic Issues

Interesting… I don’t know if I got the bonus question right or not, but now I am finding some interesting things about it. It turns out that I am both right and wrong about the properties of taking the modulo of a negative number.

I did quite a bit of testing to make sure that when you take the modulo of a negative number, you receive either a negative number or zero. This was true whether I used php, emacs, bc, or an online calculator I found.

However, apparently when programmers were first writing the modulo routines they decided on a slightly different approach as to what modulo actually meant and came up with the current solution, which generates a negative number. Hence:

The mathematical definition:

http://en.wikipedia.org/wiki/Modular_arithmetic

The Computer Science definition:

http://en.wikipedia.org/wiki/Modulo_operation

So, looking at these there is a difference between the two, but I favor the computer science version in this case since we are dealing with indexes that are to be used on a computer system where this is defined in the CS method.