Can someone explain why this is desired behavior? I want to iterate through an array backwards and wrap the index counter around to the last index of the array using modulo. This is pretty common desired behavior of modulo.
It’s correct behavior. The result of modulo is remainder (to next full number) of dividation, if you divide negative number to positive number and vice versa, the result will always be negative… including the remainder and same as divide it will be always directed toward 0.
-1/4 is -0.25 (not -0,75), so -1%4 will be -1
You will need to create function which in case of negative input it will Abs the result of module and using it substract 2nd number:
this has been an issue for me too. wondering if there’s a good reason for doing it this way. if there is, maybe they could offer alternative version of modulo.
My bad then…I thought that always positive is the common behaviour of modulo but seems I was wrong.
It seems that in fact it varies between programming languages but in most of them the remainder has the same sign as the dividend. Source: Modulo - Wikipedia
Edit: Please excuse the non-academical source.
Ok, I see, I was used to other gameplay scripting environments I’ve been in with lua and python where the b-Abs(a%b) behavior is the default. Thanks for clearing that up, I guess I’ll make a convenience function for it. I begrudgingly accept your answer Cheers
It is implemented differently across different languages. Some use the mathematical definition (which you are expecting), some do not (C++ is one that does not).
To get a true mathematically accurate modulo operation using the C++ % you can do:
I don’t know if it’s fair to say all of “computing” behaves that way, as that article lists a number of languages that perform the mathematical operation. But obviously C++ (on which Unreal is based) works that way. This discrepancy is what causes so much confusion, because most people think of % in an objective mathematical context rather than a language specific paradigm.
First of all, the % operator is the “remainder” operator in both Unreal Engine and Unity, rather than the “modulo” operator. It does not correct for negative values, so you are not guaranteed to return non-negative values with only the % operator.
There are some different formulas being posted here. I set up a test for a few different values (Unreal Engine and Unity turned out to be equivalent in these tests):
**If we define our desired behavior to be “wrapping around an interval” (going from 0 to the divisor; 10 in the following examples), then the following equations should hold: **
3 % 10 = 3
(-3) % 10 = 7
13 % 10 = 3
(-3) % (-10) = ?
Regarding the last point here, most formulas seem to yield the same sign as the divisor, regardless of mathematical meaning, so I’ll leave that fringe case open for interpretation.
I will refer to the variations of the values in this table as ++, ±, -+, and --, mapping to the sign of the a and b variables, respectively. I.e. the dividend is the former of these symbols, and the divisor is the latter. E.g. if a = 3 and b = (-10), then their shorthand symbols would be “±”.
b - Abs(a % b)
Relatively cheap to use; only one [%], one [-], and one [Abs].
Overcorrects with “++”.
Correct with “-+”.
Overshoots with “–”.
Overshoots with “±”.
**Good for correcting for negative dividents, but nothing more. **
(a % b + b) % b
Relatively expensive to use; two [%] and one [+].
Correct with “++”.
Correct with “-+”.
“–” mirrors behavior of “++”.
“±” mirrors behavior of “-+”.
**Safest option, but expensive. **
a % b + b
Cheapest to use; only one [%] and one [+].
Overcorrects with “++”.
Correct with “-+”.
“–” mirrors behavior of “++”.
“±” mirrors behavior of “-+”.
**Most performant on “-+”, but incorrect with like-signed configurations ("++" or “–”). **
TL;DR
Use this formula to be safe in all cases: "(a % b + b) % b"
Use this formula to be safe if divisor is positive: "b - Abs(a % b)"
In the formulas discussed, the result will have same sign as the divisor (bottom part of the fraction).