Home

2024

Worklog

LETSGO Game

Modulo Operator for Magic Math

Modulo Operator for Magic Math

Date
November 27, 2024
Hours
1
Tags
UnrealCodeC++LETSGO

In the majority of technical interviews I’ve ever done, there’s always a question where the answer requires you knowing the % operator exists.

This modulo operator divides, but gives you the remainder instead of the actual value.

Ladies and gentlemen (and others), I am pleased to announce that I have used the modulo operator in actual code for the first time in my decades of experience programming.

Now, I could whinge on the absolute STATE of technical interviews, but we already know it. Suffice to say knowledge of % has only ever failed out self-taught programmers.

Anyways, I used it today:

Used it in a fairly magical way:

// This (Beats / 4) is somewhat magical, but works.
// At least if the time signature is 4/4
switch (b % (Beats / 4)) { 
	// ... 
}

I’m detecting the remainder of a variable modulus divisor, where b is the current beat and Beats is the total number of beats in a bar (which will be one of 4, 8, or 16).

I switch on the result which allows me to find how strong a beat is- beat 1 and 5 are stronger than 2 and 6 in a 16 note bar.

… If this is all nonsense to you, don’t worry, it is, but it works.