A group of us in the library are learning python together using the codecademy python course. When we got to the lesson about Modulo (mod, %), a few people asked why? How often does the remainder come up, that it gets its own arithmetic symbol? Quite often, as it turns out!
That evening, I was working on a pair of costume goggles for an upcoming parade, and noted that my code made heavy use of the mod operator:
Each eye of the goggles is made up of 16 “pixels”, labeled 0-15 (left eye) and 16-31 (right eye). For each frame of the animation on the left eye, there are five pixels lit up:
Frame 0: 0,1,2,3,4
Frame 1: 1,2,3,4,5
Frame 2: 2,3,4,5,6
...
These are somewhat easily computed. For frame number k, we want to light up the pixels whose names are: k, k+1, k+2, k+3, k+4. That’s straightforward enough. But what about when you reach the end of the numbering for the eye?
Frame 11: 11,12,13,14,15
Frame 12: 12,13,14,15,16
Frame 13: 13,14,15,16,17
Frame 14: 14,15,16,17,18
This wouldn’t cause any errors, but wouldn’t be exactly what we wanted. The trail would not continue around the left eye like it should, but would spill over onto the right eye. That might look pretty cool too, BUT what we really want is for these pixels to be the ones lit up:
Frame 11: 11,12,13,14,15
Frame 12: 12,13,14,15,0
Frame 13: 13,14,15,0,1
Frame 14: 14,15,0,1,2
We could write fancy exception code to handle the boundaries, or we can use the modulo operator: instead of using k, k+1, k+2, k+3, k+4 we can use k%16, (k+1)%16, (k+2)%16, (k+3)%16, (k+4)%16. This will make the smaller numbers act in the expected way (for numbers k < 16, k%16 is just k) and larger numbers will turn into the numbers we want (e.g. 16%16 = 0, 17%16 = 1, etc.)
What happens for larger frame numbers, like 25? or 250? does the code still work?
What should the code / math be for the right eye?
Check out the actual goggle code at my github.













