Live data from Hacker News

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

twitter.com

11–20 of 291 posts

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

#12
post #6

> it takes me ages to convince myself this implementation is correct It would take them less time to convince themself if they switched min and num to put the values in proper semantic order: min Math.min(Math.max(min, num), max)

That still leaves the order of Math.min and Math.max undecided and will probably not help much if you get easily confused by the visuals of this code.

I never thought about it but of course there must be code tongue twisters (or more correctly, brain twisters).

Thinking about it, I would probably go with a less confusing implementation. Terse code is hard to read and the compiler is likely clever enough to choose the best implementation anyway.

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

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

I don't find this a whole lot easier to read to be honest. It seems like doing minification manually, when we have tools to do that for us. An if statement seems a lot clearer, and minifies well https://twitter.com/jaffathecake/status/1296423819238944768

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

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

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

#16
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

Now in C# with pattern matching:

    int clamp(num, min, max)  
    {  
      return num switch  
             {  
               _ when num > max => max,  
               _ when num  min,  
               _ => num  
             };  
    }
Or as a lambda:

    Func clamp = (num, min, max) => num switch {_ when num > max => max, _ when num  min,_ => num};

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

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

I don't find this a whole lot easier to read to be honest. It seems like doing minification manually, when we have tools to do that for us. An if statement seems a lot clearer, and minifies well https://twitter.com/jaffathecake/status/1296423819238944768

It's just a matter of what you're used to. Both of those are equally clear to me.

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

#19

Always good to know the underlying implementation, but for any Ruby readers check out clamp(). It's been available since 2.4. 25.clamp(5, 10) => 10 6.clamp(5, 10) => 6 1.clamp(5, 10) => 5

I find myself needing this most frequently in making graphics in R. The scales package has squish() with the same behavior:

squish(25, c(5, 10)) => 10

squish(6, c(5, 10)) => 6

squish(1, c(5, 10)) => 5

If you don't provide the limits it defaults to c(0, 1). That's because this function exists to map to a 0-to-1 range for functions that then map the [0, 1] range to a color ramp.

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

#20
post #18
post #15

[min, num, max].sort()[1]

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.
Post reply on HN