# Keep an Int in Range (Over/Under Flow)

**URL:** <https://forums.unrealengine.com/t/keep-an-int-in-range-over-under-flow/469007>\
**Category:** Modding\
**Tags:** unreal-engine, question\
**Created:** [July 31, 2020, 9:56pm UTC](https://forums.unrealengine.com/t/keep-an-int-in-range-over-under-flow/469007 "2020-07-31T21:56:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![An\_anonymous\_man](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@An\_anonymous\_man](https://forums.unrealengine.com/u/An_anonymous_man)\
**Post date:** [July 31, 2020, 9:56pm UTC](https://forums.unrealengine.com/t/keep-an-int-in-range-over-under-flow/469007/1 "2020-07-31T21:56:38Z")

</div>

I have a need of wrapping an int to a specific range, via integer underflow. For example, if the value is greater than x then set it back to its minimum value. I was able to do that part easily enough with a modulo/int overflow (x % max x value). I don’t know, however, how I should go about wrapping it back to its max value when it is lower than its minimum.

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [July 31, 2020, 11:36pm UTC](https://forums.unrealengine.com/t/keep-an-int-in-range-over-under-flow/469007/2 "2020-07-31T23:36:41Z")

</div>

Just do it manually. And % works if the range starts with 0, it won’t work if the range is, for instance, [1, 6] or something;

If the range starts with 0, it should be X % (Xmax + 1), shouldn’t it? If the range is [0, 5], 5 % 5 = 0, and 5 % 6 = 5.

But overall, this is what I’d do:

 ![308820-int.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/f/b/dfbc60227fb2db90a69eefae10740928ccac5f7f.png)

---

<div class="post-metadata">

**Author:** ![An\_anonymous\_man](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@An\_anonymous\_man](https://forums.unrealengine.com/u/An_anonymous_man)\
**Post date:** [July 31, 2020, 11:51pm UTC](https://forums.unrealengine.com/t/keep-an-int-in-range-over-under-flow/469007/3 "2020-07-31T23:51:20Z")

</div>

Thanks for the response, I guess that is the easiest way to do it. I am using the modulo for a 0 based index, I just thought there might’ve been a way to calculate it for the opposite direction/underflow.
