The basic function is simply defined as: function clamp(num, min, max) { return Math.max(min, Math.min(num, max)); } That is, if you don't try to do anything fancy and make any parameters optional. Lodash does and it makes the implementation much more complex. _.clamp(input: number, lower?: number, upper: number): number; https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb7...
Or to make sure it's crystal clear what's going on: function clamp(num, min, max) { if (num > max) return max; if (num
Math.min(Math.max(num, min), max)
21–30 of 291 posts
Re: Math.min(Math.max(num, min), max)
#22Earlier quoted context omitted.
sort() on JavaScript arrays sorts alphabetically (unless you pass a compareFunction) https://stackoverflow.com/questions/21019902/why-cant-javasc... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Very good point. I thought of it more as pseudocode.
Re: Math.min(Math.max(num, min), max)
#23[min, num, max].sort()[1]
I really love this! I’m not sure whether to laugh, cry, or applaud, but I love how it makes me feel all those emotions at the same time.
Re: Math.min(Math.max(num, min), max)
#24In languages I use there’s usually no need to write that code. C++/17 has std::clamp() in header. Modern C# has Math.Clamp() since .NET Core 2.0; too bad it’s not available in desktop edition of the runtime. HLSL has clamp() intrinsic function, and a special version saturate() to clamp into [ 0 .. +1 ] interval.
Re: Math.min(Math.max(num, min), max)
#25Re: Math.min(Math.max(num, min), max)
#26Earlier quoted context omitted.
Or to make sure it's crystal clear what's going on: function clamp(num, min, max) { if (num > max) return max; if (num
Seriously. This is about a million times better.
Re: Math.min(Math.max(num, min), max)
#27In languages I use there’s usually no need to write that code. C++/17 has std::clamp() in header. Modern C# has Math.Clamp() since .NET Core 2.0; too bad it’s not available in desktop edition of the runtime. HLSL has clamp() intrinsic function, and a special version saturate() to clamp into [ 0 .. +1 ] interval.
Great. But I have been programming since about 1985.
For example, C++ on AMD64 is very likely to compile std::clamp into 2 instructions, minsd and maxsd. I’m not so sure about nested ternaries mentioned elsewhere in the comments.
Re: Math.min(Math.max(num, min), max)
#28I find the following easier to read :
Math.min(Math.max(num, lower_bound), upper_bound)Re: Math.min(Math.max(num, min), max)
#29Stage 1 ECMAScript proposal to add Math.clamp (among others): https://github.com/rwaldron/proposal-math-extensions But it looks dead: https://github.com/rwaldron/proposal-math-extensions/issues/... As someone mentioned in the thread, nested ternary is easier to interpret: (a > max ? max : (a < min ? min : a))
Re: Math.min(Math.max(num, min), max)
#30I find that the fact that the functions min and max have the same name as the variables min and max increases cognitive load which makes it harder to think about it. I find the following easier to read : Math.min(Math.max(num, lower_bound), upper_bound)