When we first learned long division, we discovered that not all division problems came out evenly. For that first year, we were told to report the undivided portion as the remainder. In the next year, a solution was offered, along with the introduction of fractions – just add a fraction created from the remainder and the divisor. With the introduction of decimals, we learned how to continue the division to any desired accuracy – a system that our computers follow even now. After the introduction of decimal division, it may have seemed as though the remainder was baby stuff, to be left behind forever.
T’aint true, however. In “adult” mathematics, the lowly remainder reappears with the concept of “modulus.” It is a useful concept, with a number of applications in programming, and I’ll discuss its use and implementation here.
How to Get It
On some computers, there is a function, MOD, that will return the remainder of a given division problem. It is invoked by LET N=x MOD y. This would set N equal to the remainder of x divided by y. While the Timex does not have a MOD function, we can determine when the MODULO equals zero by comparing (x/y) with INT (x/y). If x can be evenly divided by y, (x/y) will equal INT (x/y), as (x/y) will have no decimal component.
If the actual value of the remainder is needed, we can calculate this remainder: since the decimal fraction represents the remainder divided by the divisor, by multiplying the divisor back in again, we can “reconstitute” the remainder. First we isolate the decimal fraction, using ((x/y)-INT (x-y)); then we multiply by the divisor: “*y.” The formula in BASIC that does this is: LET N=((x/y)-INT (x/y))*y.
Where to Use It
In fact, the Timex uses modulo in two places; RND is generated using a formula that involves modulo calculations, and TAB is reduced MOD 32 (try PRINT TAB 50; “HI”). Neither application can be adapted to other uses, however. To do that, we’ll have to use the formula that appeared above.
Let’s start at the bottom, with modulo 2. Try:
10 FOR X=1 TO 10
20 PRINT X,((X/2)-INT (X/2))*2
30 NEXT X
You should get:
1 1 2 0 3 1 4 0.... etc.
Note that even numbers Modulo 2 are equal to zero. We now have a way to sieve even from odd!
Now change the “2” in the above program to “3”. You should now get:
1 1 2 2 3 0 4 1 5 2 6 0.... etc.
By changing the number, you can construct a formula that will return to zero at any regular interval. There are a number of useful intervals that can be used. By looking at a series modulo 12, you might keep track of months, but pause for a yearly summary every twelve months.
A major use of modulo is in converting between number bases. At times when dealing with the Timex, you will deal with Binary, Decimal, Hexadecimal, and what one writer has dubbed 256-imal. If you would like to convert numbers less than 256 to hexadecimal, you must reduce the number modulo 16. To find the two byte values for addresses in the Z-80 environment, the decimal address must be reduced modulo 256. To re- assemble the two bytes into a single decimal number, you must multiply the high-order byte by 256, and add it to the low-order byte. Since the low-order byte represents the remainder of a division, it is not multiplied by anything. An example is calculating RAMTOP on the 1000: PRINT PEEK 16388+256* PEEK 16389.
If you look over past programs, you will see a number of lines that contain the (x/y)- INT(x/y) idea. Each of these is using the modulo, that, as the remainder, you thought was left behind in fourth grade!
Thanks to The Algorithm Corner, in June ’85 Computer Trader Magazine, for the idea.
MF
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.