Sorry for the 5-year necroposting, but I been googling furiously for half an hour to figure out how Unreal handles negative numbers on modulo. And this thread is the only relevant result.
The behavior of mod is highly inconsistent over different languages and math libraries.
And I agree the backwards wrapping variant is the most convenient one.
Unreal Engine uses the same modulo as in C++, and C++ rounds toward zero. What does that mean?
Well, the modulo formula is ((a/b) - round(a/b)) * b. When we round in the “normal” modulo, we round towards negative infinity.
So, this explains why UE4’s mod is kinda weird:
-1%4
Normal mod: ((-0.25) - (-1)) * 4 = 3.
C++ mod: ((-0.25) - (0)) * 4 = -1.
Hope it helped.
If someone wants a looping modulo solution that resembles how it calculates in Excel or whatever, this is the simple Pure Function I’ve kept in my function libraries for 3 years now. And I’ve included an example of how I use it to help cycle forward and backward through a character’s abilities list.