BoS - Extra Shaping Functions: Mod()

22nd Nov, 2021

“Mr. Miyagi's fence lesson” is really long. But I supposed Daniel-san also spent some weeks painting those fences and waxing that car.

Modulo, % or mod()

Modulo finds the remainder of a division. It is abbreviated as mod, or represented by %.

Example:

If we plot y = mod(x, 0.5):

Why this sawtooth result? mod(a, b) returns the value of a modulo b. This is computed as a - b * floor(a/b). Lets calculate 0.1 % 0.5:

Other values of x:

What this means is that modulo will give you the remainder of a division, and when you graph that it translates as a rising line. The remainder keeps getting bigger and bigger until eventually you divide the number by itself, resulting in remainder 0.

So, when you do y = mod(x, b), b is how much the line will rise before it resets to zero.

Example:

y = mode(x, 1.0)

y = mode(x, 1.5)

y = mode(x, 2.5)