On Mon, Feb 23, 2009 at 7:36 AM, Jussi Toivola <[email protected]> wrote: > Hmm... I'd try something like this: > > print 13 % 12 > . . . yields 1. > > print -1 % 12 > . . . yields 11. > > print 11 % 12 > 1 % 12 > . . . asks if 11 is left of 1 and returns True. > > print ( 315 + 360 ) % 360 > . . . yields 315 > > print 666 % 12 > . . . yields 6... :o > That works for numbers clamped between 0 and N, what about ones that are clamped between N1 and N2?
class Ring(object): def __init__(self, low, upper): self._offset = low self._diff = up - low def clamp(self, value): shifted = value - self._offset return shifted % self._diff + self._offset The above takes into account your suggestion but allows offsets other than 0.
