Live data from Hacker News

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

twitter.com

191–200 of 291 posts

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

#191

OPs implementation requires 2 comparisons. I think this C expression is clearer, and will sometimes use 1 comparison: num max ? max : num

To me the danger of this kind of one liners is that if you use it 2 times somewhere, someone, at some point, will do the lazy refactor of “let’s put it into a function”.

  int clamp (int value, int min, int max)
And that one liner inside. And now you have a double evaluation bomb waiting to go out on your codebase.

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

#193

OPs implementation requires 2 comparisons. I think this C expression is clearer, and will sometimes use 1 comparison: num max ? max : num

To me the danger of this kind of one liners is that if you use it 2 times somewhere, someone, at some point, will do the lazy refactor of “let’s put it into a function”. int clamp (int value, int min, int max) And that one liner inside. And now you have a double evaluation bomb waiting to go out on your codebase.

Why? You wrote a function, not a macro.

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

#195

Earlier quoted context omitted.

I often see [a, b, c].sort[1] which I think is very neat.

If you are writing code that is clamping a lot of numbers, the GC churn from building a new array every time might be a problem.

Ideally the temporary array should optimise away.

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

#196
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)

That doesn't really help. "Max" to enforce a "lower bound" is briefly halting.

Maybe add an alias to Max called AtLeast and one for Min called AtMost to be used in these situations ;)

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

#198

OPs implementation requires 2 comparisons. I think this C expression is clearer, and will sometimes use 1 comparison: num max ? max : num

Incidentally, gcc and clang both compile this to two cmovs for me.

I don't speak x86. Do you mean that these compilers are evaluating both conditions, all the time? At what optimization level?

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

#199
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)

Assuming we know (lower_bound Math.max(lower_bound, Math.min(num, upper_bound))

Since i read right to left.

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

#200
post #45

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.

It gets a bit confusing when the order of arguments is different depending on the library. For instance, with std it's std::clamp(val, min, max), but with Qt it's qBound(min, val, max) (for some reason I think the order of arguments in qBound is more logical).

The expression Math.min(Math.max(num, min), max) is symmetric in min and num, so it doesn’t matter whether you interchange min and num (or, for that matter, max and num, but that is harder to see from that way to define the ‘clamp’ function)
Post reply on HN