c++ - How can I use modulo to do REVERSE (backwards) wrap-around (overflow)? -
can look ((x-1) % k + k) % k simplified further?
this more of math question is programming question, shows in coding. suppose iterate on calendar's months. can shown numbers as:
int this_month = 5; int next_month = (this_month+1) % 12; where both integers between 0 , 11 (inclusive). thus, whole number w restricted range r = [x,y], "overflow," lack of improve term, of w on r w % (y-x+1) + x. causes wrap around if w exceeds y.
what if want backwards?
int last_month = ((this_month-1) % 12 + 12) % 12; is there ingenious , elegant way simplify look ((x-1) % k + k) % k seen in code above?
now changing range of months [1,12] simplify stuff, when dealing matrices, arrays, etc. index numbers begin @ 0, can't alter range.
k % k 0. i'm not 100% sure you're trying seems want lastly month clamped between 0 , 11 inclusive.
(this_month + 11) % 12 should suffice.
c++ modulo algebra
No comments:
Post a Comment