title: Math Looping Between Min and Max Using Mod?
body: <p>i'm attempting to build a tiny (or perhaps not so tiny) formula that will contain numbers between a set min and max, but also loop these numbers so they are not clipped if they are outside of the range.  so far, this is what i have.</p>
  
  <pre><code>min1 = 10
  max1 = 90
  
  val1 = 92
  //will make 11, which is what i want since it loops
  
  formula:  min(max(min1,min(val1,max1)),mod(val1,max1)+min1)
  </code></pre>
  
  <p>however, i'd like it to loop the other direction also, so that if val1 is 5, which is -5 outside of min1, it will become 86.</p>
  
  <p>another problem i'm running into is that</p>
  
  <pre><code>max1 % max1 != max1
  </code></pre>
  
  <p>as i want it to, since the max is part of the range</p>
  
  <p>trying to be clear, here are some examples of desired output based on a range with looping</p>
  
  <pre><code>min1 = 10
  max1 = 90
  ----------------------------------------------
  val1 = 30    //within range: stays as 30 
  val1 = 90    //within range: stays as 90
  val1 = -6    //below range: loops to becomes 75
  val1 = 98    //above range: loops to becomes 17
  val1 = 91    //above range: loops to becomes 10
  </code></pre>
  
  <p>i'd like not to resort to using a series of if/else statements, but one would be fine if it's absolutely required.  is that even possible?</p>