Why division remainder and modulus are not the same

It’s simple really. What’s (4-6) mod 12? There’s two ways of thinking about this: the right way and the wrong way.

One is modular arithmatic. If you don’t know what that is, think of a clock: if it’s 5 o’clock, one hour later will be 6 o’clock. Ten hours after this will be 4 o’clock, not 16 o’clock (lets stick with a 12-hour clock, OK?). So what was the time 6 hours before 4 o’clock? 4 – 6 = 10 mod 12 — we can’t represent negative numbers on a 12-hour clock, so we need to add 12 until we get a positive number. “5 mod 12” is just a mathematical way of saying we don’t know how many days in the future (or past) and we don’t know whether it was day or night, but we do know it was 5 o’clock.

So what’s the other way of thinking about -2 mod 12? Well, notice that for positive numbers, x mod y is the same as the remainder of x/y, for example 20/12 is 1 remainder 8 — if you’re a programmer, you might write this as 20%12. Now you see what I’m getting at. And it’s not too hard to see why this might be a problem either. Say you calculate the hour on a clock, and store the positions to draw the hand in an array of length 12 — you need an index in the range [0,11], so (4-6)%12 is not much good if it returns -2 (or worse, in C90 and C++03, there’s no guarantee what the % operation will yield). It’s nice to see that both perl and python interpret “-2 % 12” as “(-2) mod 12” (which I think is far more useful than “(-2) remainder 12”), although it’s all a bit confusing since in Java and C♯ and usually (AFAIAA) in C++ “-2 % 12” will result in -2 (see wikipedia for a list).

So why am I pointing this out? Because I’ve seen so many programming languages implement “x % y” as a remainder operator and call it a “mod” operator. And it’s just not. If you right “day_of_week[day % 7]” you need to be careful that “day” is not negative, or your program will probably seg-fault or do something funny (or throw an exception in safe languages). If you actually used modular arithmatic, on the other hand, it would handle negative days just fine. And yet I’ve seen this kind of code so often, and been stung several times myself when I thought the “day” was non-negative, that in the end I just decided to use “mod” even though it’s usually slightly slower.

Advertisements

About dhardy

A software developer who landed in Switzerland, I love conjecturing over a few things computer-related, open collaboration, and quietly promoting linux/KDE as a desktop OS.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s