Hey guys, I’m new to programming and I really hit the wall here. I know what modulo or modulus is but my comprehension falls short in this case. BTW this is exactly taken from the puzzle game template. I want to figure out how loops work and how can I iterate an objects n times but don’t understand what that modulo node does here. I know it is a simple and stupid question but I really want to learn this stuff.
Good example case for modulo is splitting items in a row. Lets say we have 20 items that needs to be placed in rows of 6 items. X modulo 6 = Y in loop (X =0-19, Y=remainder) would output numbers between 0 and 5 which we can use to populate each row
modulo returns the remaining number of a division. for example: 20 % 8 = 4.
step1: 20 / 8 = 2
step2: 2 * 8 = 16
step3: 20 - 16 = 4
the algorithm is basically: A - (B * Floor(A / B)). Floor() rounds down a number. so instead of 20 / 8 = 2.5, it rounds it down to 2.0. hope its clear now.
that’s a little inaccurate. it’s based on how the % operator in c++ works, which just truncates the decimal portion of the number. this works the same as flooring when A and B are both positive or both negative but not when one is positive and the other’s negative.
a more accurate equation would be A - (B * floor(A/B)) + (B/2 * (sign(A) - 1)), with the exception that when A = 0 it returns -B/2 instead of 0
if that’s not your desired behaviour, i recommend making a macro based on one of the other modulo variants Modulo - Wikipedia