title: Most efficient/elegant way to clip a number?
body: <p>Given a real (n), a maximum value this real can be (upper), and a minimum value this real can be (lower), how can we most efficiently clip n, such that it remains between lower and upper?</p>
  
  <p>Of course, using a bunch of if statements can do this, but that's boring! What about more compact and elegant/fun solutions?</p>
  
  <p>My own quick attempt (C/C++):</p>
  
  <pre><code>float clip( float n, float lower, float upper )
  {
      n = ( n &gt; lower ) * n + !( n &gt; lower ) * lower;
      return ( n &lt; upper ) * n + !( n &lt; upper ) * upper;
  }
  </code></pre>
  
  <p>I'm sure there are other, better ways to do this, that's why I'm putting this out there..!</p>