title: What's the most elegant way to cap a number to a segment?
body: <p>Let's say <code>x</code>, <code>a</code> and <code>b</code> are numbers. I need to limit <code>x</code> to the bounds of the segment <code>[a, b]</code>.</p>
  <p>In other words, I need a <a href="https://math.stackexchange.com/q/1336636">clamp function</a>:</p>
  <pre><code>clamp(x) = max( a, min(x, b) )
  </code></pre>
  <p>Can anybody come up with a more readable version of this?</p>