# Modulo does not handle negatives as expected

**URL:** <https://forums.unrealengine.com/t/modulo-does-not-handle-negatives-as-expected/44896>\
**Category:** Blueprint\
**Tags:** unreal-engine\
**Created:** [October 17, 2015, 1:34am UTC](https://forums.unrealengine.com/t/modulo-does-not-handle-negatives-as-expected/44896 "2015-10-17T01:34:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_f778cf8f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f778cf8f](https://forums.unrealengine.com/u/anonymous_user_f778cf8f)\
**Post date:** [October 17, 2015, 1:34am UTC](https://forums.unrealengine.com/t/modulo-does-not-handle-negatives-as-expected/44896/1 "2015-10-17T01:34:05Z")

</div>

Hi, I am having trouble getting attention to what I feel is a kind of fundamental problem with blueprint math. Can someone from Epic please comment on this please? [Blueprint modulo node doesn't handle negatives? - Programming & Scripting - Epic Developer Community Forums](https://answers.unrealengine.com/questions/212021/blueprint-modulo-node-doesnt-handle-negatives.html)

> [@](#):
>
> 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.

Thank you

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4ff1a46a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4ff1a46a](https://forums.unrealengine.com/u/anonymous_user_4ff1a46a)\
**Post date:** [March 4, 2020, 2:05pm UTC](https://forums.unrealengine.com/t/modulo-does-not-handle-negatives-as-expected/44896/2 "2020-03-04T14:05:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f9e5f063](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f9e5f063](https://forums.unrealengine.com/u/anonymous_user_f9e5f063)\
**Post date:** [June 13, 2020, 11:39am UTC](https://forums.unrealengine.com/t/modulo-does-not-handle-negatives-as-expected/44896/3 "2020-06-13T11:39:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nochehalcon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nochehalcon](https://forums.unrealengine.com/u/nochehalcon)\
**Post date:** [January 17, 2026, 10:17pm UTC](https://forums.unrealengine.com/t/modulo-does-not-handle-negatives-as-expected/44896/4 "2026-01-17T22:17:35Z")

</div>

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.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/c/c/0cc8ede18347783d018c6783060571c67cba87a8.png)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/9/9/8995904eb9f309579ddf763ed1523817807df269.png)
