Last active
October 31, 2018 05:00
-
-
Save Jeff-Russ/0ca7e6c936626746acae0008e627da4d to your computer and use it in GitHub Desktop.
C/C++ function to wrap out-of-bounds numbers to a specific range (modulo with an offset)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* wrap_to_range() is similar to modulo but, instead of wrapping back to zero, wraps | |
back to specified range closer to the top (what the modulo value would be). | |
It was created for musical keyboard to handle a note command that is too high to be | |
played in order to handle that event that so that the specified note is still played | |
just in a different octave (the highest octave possible). | |
For those where that useage doesn't ring true, imagine a clock function (in 24 hour | |
time) where if 24:01 is attempted to be set (an invalid time) it is adjusted to 23:01. | |
The same 23:01 would be returned if 25:01 or 26:01 are attempted. | |
The second arg is the max (allowed) value | |
The third arg is the width of the wrapping | |
For unsigned results the 3rd arg CANNOT be greater than max (2nd arg) */ | |
long long wrap_to_range(long long val, long long max, long long width) | |
{ | |
if (val <= max) return val; | |
long long offset = max - width; | |
val -= offset; | |
val = val % width; | |
val += offset; | |
return val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A version that wraps the integer component, keeping floating point component where it is: