I am in need a of a function that computes the sum of unsigned val
and signed dX
and wraps the result around the range lower
and upper
For example:
A value of 5
, change of -6
, and a range of 0
and 10
would return 10
.
< 1 2 3 4 5 6 7 8 9 10 >
A value of 2
, change of 3
, and range of 1
and 3
would return 2
/*
* Given a value and a change in that value (dX), find the sum and wrap it between lower and upper.
*/
unsigned int wrap(unsigned int val, const int dX, const unsigned int lower, unsigned int upper)
{
}
I don't really know how to approach the unsigned and signed addition/subtraction to avoid underflow. Nor am I sure exactly how to wrap the lower bound.