News for hackers. Stuff that matters.
Re:Math.min(Math.max(num, min), max) by tobr on Thursday August 20, 2020 @03:26PM (#24227593)
In practice “max” and “min” often aren’t conceptually important for a clamp function. It’s more that you want to keep a value within a range, which is defined by two end points in arbitrary order.

If that is the version of clamp you need, the sort based solution reveals something profound and unexpected: it’s not just the two end points of the range that are equivalent, but all three numbers. Keeping value A between B and C is the same as keeping B between A and C or C between A and B. It’s completely arbitrary which pair you consider to be a range.

There are no comments on this comment yet.