Modulo
The
remainder of the division is a simple—looking thing, but useful to the point of outrage. In the CalculationLaboratory, it is calculated using the function
mod(a, b).
Or, to put it more simply, it's the same as a % b. The formula is familiar, it makes more sense than it seems.
For example, you divide 7 by 3. It turns out two whole times, but 1 remains. This "1" is the remainder. Sometimes we ignore it, but in programming it is sometimes more important than the result itself.
Why do we need the rest at all? Let's be honest: there are a lot of places.
Do you want to know if it's an even number or an odd number? n % 2. If the result is 0, it means even. That's it.
Are you arranging the elements in a circle? The remainder is useful as a counter, which is "reset" after each lap.
Are you making a calendar? day %7, and you already know what day of the week it will be.
Are you following the rhythm of the functions? The remainder helps to catch the periodicity, especially in trigonometry.
In short, mod is like a programmer's Swiss knife. One line helps with logic, loops, and template markup. He often remains in the shadows, but without him, half of the decisions would be much more confusing and lengthy.
So yes, it seems to be a simple thing. But if you own it, you can solve a carload of problems without racking your head.
Help on built-in functions
The rules of programming scripts
Programm options
Color constants