47 ms·
I find the following much easier to read. It's certainly easier to maintain, deal with, for successive programmers, and likely has exactly the same compiled/int
by ChrisLomont 6y ago
I find the following much easier to read. It's certainly easier to maintain, deal with, for successive programmers, and likely has exactly the same compiled/interpreted operations for most common languages. Optionally replace t1 and t2 by overwriting num if needed.
Use full if else if you must.
If you're worried about speed, using built in min and max is not a good idea. There are many, many tricks to remove branches for certain datatypes, etc., as you need.
var t1 = num < min ? min : num; // clamp to min
var t2 = max < t1 ? max : t1; // clamp to max
return t2; // num clamped to [min,max]