Live data from Hacker News

Math.min(Math.max(num, min), max)

twitter.com

21–30 of 291 posts

Re: Math.min(Math.max(num, min), max)

#21
post #5
post #2

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

Speaking only to JS is there any reason to write it any other way outside of being clever or as a lambda for singular use? I definitely prefer this version. (Assuming any necessary runtime checks are included for a given project)

Re: Math.min(Math.max(num, min), max)

#22
post #20
post #18

Earlier 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.

imho a very elegant solution and probably works in most languages, I was surprised by JavaScript's behaviour myself

Re: Math.min(Math.max(num, min), max)

#24

In 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.

Re: Math.min(Math.max(num, min), max)

#26
post #5

Earlier 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.

Absolutely. With the above implementation, I can see exactly what's going on, with the others, I'm trying to work out potential edge cases.

Re: Math.min(Math.max(num, min), max)

#27

In 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.

I’ve been programming for living since 2000, but I don’t think that’s relevant. No reason not to use what’s available in standard libraries of whatever language you’re writing.

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)

#28
I 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)

Re: Math.min(Math.max(num, min), max)

#29
post #7

Stage 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))

To each his own but I disagree. Nested ternary's are hard to read and understand, and modifying them (by future devs) is tricky and error prone.

Re: Math.min(Math.max(num, min), max)

#30
post #28

I 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)

I think this was posted purely for the limerick quality.
Post reply on HN