Understanding Modulo
I can’t think of a person that isn’t aware of modular counting. We do it just about every day… well, hopefully not while we are on vacation. But every time you look at your watch, and figure out how many hours you have left before your work day is over, and it is before noon, you do it. Every time you have to time your goodies in the oven, or on the stove, you probably have to figure out some time past the top of the hour. Microwaves even make us do it… the kids like corn dogs (are they dorkfish?), and the directions state 70 seconds for the first one, and 40 seconds for each additional. So, they have to figure out the time in number of minutes and seconds… This is called modular counting.
I will place a table of calculations here that hopefully help to understand how Ruby uses the ‘%’ method for class Fixnum. The setup: x is a time given in 24 hour time. We only have a 12 hour clock on our wall… We want to know what the 12 hour time is. The twist? I will show you what it looks like when you have a negative 12 clock, or a negative time given, and I will show both. It gives us four options for the format of “x mod 12” in the way of positive or negative values.
The way to think of this is look at the clock. If x is positive, then you go around clockwise, like you are used to doing. When it is negative, then you go to the left. But, if the clock is negative, then all times are negative. But, the x being the hand still rotates the way as just described, either counterclockwise if negative, or clockwise if positive.
Oh, in my table, I made one minor adjustment… Because it is a clock, I did account for 12 noon… Otherwise you would end up with 0 hour in the middle of the table… and we all know that midnight is 0 hour, noon is the 12th hour. It also solves having a negative 0 in my table which was ruining the clock analogy for this explanation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|