# Rectification maps values to an limits treated either as cycles, upper # bounds, lower bounds or both. Four methods are added to the Numeric class: # # wrap_rectify() wraps values to an interval treated as a cycle. Think of it # as degrees around a compass. # # cut_rectify() cuts off values to keep them between an upper and lower bound. # # at_most() cuts off values at an upper bound. # # at_least() cuts off value at a lower bound. # Behavior added to Numeric by eymiha_math. class Numeric # Treats the interval between low and high as a cycle, returning the result # of wrapping the instance around this interval. def wrap_rectify(high=1,low=0) if (high != low) low,high = high,low if low > high diff = high-low self-diff*((self-low)/diff).floor else low end end # Treats the low and high as limits, returning low or high if the instance # is respectively below or above these values, or the instance if between # the two. def cut_rectify(high=1,low=0) low,high = high,low if low > high (self < low)? low : (self > high)? high : self end # Constrains a value to an upper limit. def at_most(high=0) self > high ? high : self end # Constrains a value to a lower limit. def at_least(low=0) self < low ? low : self end end