Live data from Hacker News

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

twitter.com

81–90 of 291 posts

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

#82
post #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.

In a file of utility hacks

near get(), post(), and ajax()

// we alias at import

// to keep all the code short

min(max(number, min), max)

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

#83
post #75
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)

Easy to remember, but may take some time to grasp: Arrays.sort( {lower_bound, num, upper_bound} )[1]; Next challenge: teach the optimizer to make that almost as fast as the min/max way ;-) (You can’t reduce it to the min/max call because it also works if you accidentally pass a lower bound that’s larger than the upper bound. Worst-case, the above takes 3 comparisons, unless at least two of the inputs are constants)

> Next challenge: teach the optimizer to make that almost as fast as the min/max way ;-)

I did exactly this for my PhD!

https://chrisseaton.com/phd/

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

#84
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...

Good point, for those who need to implement a slick compareFunction for numbers, Math.sign is great: [min, num, max].sort((a, b) => Math.sign(a - b))[1] An entirely different alternative is poor man's match: switch (true) { case num > max: return max; case num

Why sign though? I always write my sorts like this.

    .sort((a, b) => a - b)

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

#85

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.

Great. But I have been programming since about Math.min(Math.max(n, 1900), 1985 - 1)

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

#89

Earlier quoted context omitted.

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.

I prefer the min/max-based definition for the same reason: Its easier to work out the edge cases around NaN and comparisons against NaN.
Post reply on HN