Blueprint modulo node doesn't handle negatives?

E.g. -1 % 4 does not return 3, it returns -1.

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.

1 Like

Got the same problem in 4.9.1

I also ran into this issue in 4.8 and 4.9

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:

b-Abs(a%b)

1 Like

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.

Read my awnser :stuck_out_tongue: also if you follow source code of UE4, it’s using fmodf function

Which documentation say what i said in anwser, it might not be practical behavior for you, but it’s mathematicly accurate behavior:

/reference/cmath/fmod/

Create your own function or macro node if you need custom behavior or make C++ function to node

probably not using fmodf for integers. :stuck_out_tongue:

still think a built in alternative would be nice.

Maybe i looked on float node

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.

I don’t mind wikipedia. It makes more sense as both operations are related to eachother

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 :stuck_out_tongue: Cheers

Im no math genius, but after implementing my own blueprint node for positive modulus I found b - Abs(a%b) did not work

So just in case this helps anyone else I had to use (a % b + b) % b

3 Likes

Thank you ever so much for this. I was not expecting it to work and then it did!

Wikipedia is right, modulo is not division remainder. You are wrong, and C and UE are wrong too.

It is definitely not mathematically accurate behavior. Modulo should never return a negative number.

It is implemented differently across different languages. Some use the mathematical definition, some do not.

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:

(a % b + b) % b

1 Like

Thank you so much @anonymous_user_f5a50610

This fixed around 5 hours of messing around. I <3 you.

Wikiedia say otherwise:

“In computing, the modulo operation finds the remainder after division of one number by another (sometimes called modulus).”

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).
  • **Stay away from negative divisors if you can. **
2 Likes