Monthly Archives: April 2010

Gah! Java has no modulo operator!

Amazing that I’ve been programming in Java for about 15 years, and I hadn’t run into this before. Tells you just how insidious this could be. Java’s ‘%’ operator acts like a modulo operator most of the time, but it’s a remainder operator. Your formula may work just fine in C, Python, Perl, Excel, and everywhere else— until you hit a negative number.

The difference is that the range of modulo-n is 0..(n-1) for positive n, whereas the range of Java’s quasi-modulo is -(n-1)…(n-1). So x%3 in Java goes from -2 to 2, whereas everywhere else it goes from 0 to 2. If you get a negative result, you need to add 3 to get the right answer.

This comes up because I’m trying to find the quarter that a month is in. Quarters can be defined as starting in any month (e.g. the fiscal year for my church starts in July, so Q1 is July, August, and September.) But since months are a cycle of 12 (a modulo-12 number system), there are only three kinds of quarter systems: January starts a quarter, February starts a quarter, and March starts a quarter. Call them 0, 1, and 2, and call the months 0-11 (the way Java painfully does), and you can find the number of months since the start of the quarter as (month-quarterOffset)%3. As long as you’re not using Java.

The end of a rant?

I’ve had this rant for a few years now, and I repeat it every time I need to reboot my computer or phone for a system update. Why, in this day and age, should I need to reboot for a system update? The updates should just happen automatically in the background (with my permission, of course) without disrupting my work. The technology to do this has existed for decades. It’s a lot of work to get this to work seamlessly for legacy PC programs, but smart phones run apps in a more controlled environment where this sort of thing wouldn’t be difficult.

Now RIM, maker of BlackBerry smart phones, has purchased QNX, maker of highly robust real-time operating systems. Since 1980, QNX is what you used if your computer happened to be running a nuclear power plant, medical device, car, or anything else that must not fail. System updates don’t even slow it down.

The trick is that QNX is a microkernel operating system. The kernel (the part of the OS that is always running) is tiny. Traditional kernels contain all the code for handling the video display, the hard disk, the keyboard and mouse, and various other services that user-visible programs use. A microkernel is more like a traffic cop, handling messages to and from the programs that provide those services. Need to update your video driver? A new video driver program gets installed, and takes over from the old one. No reboot required. The only thing that requires a reboot is a change to the microkernel itself. And because the microkernel is so small and simple, there’s not much room for bugs to hide. And QNX has had 30 years to shake the bugs out. If RIM decides to go with a no-reboot-ever policy for system updates, they could do it.