Live data from Hacker News

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

twitter.com

1–10 of 291 posts

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

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

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

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

[deleted]

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

#4
Very coincidental I see this post almost immediately after writing the same code:

    new_poll_rate = \
        min(
            max(
                1 / messages_per_second,
                constants.FASTEST_POLL_RATE
            ),
            constants.SLOWEST_POLL_RATE
        )
I agree with the sentiment, I had to re-read this several times to make sure I got it right.

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

#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 

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

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

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

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

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

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

Seriously. This is about a million times better.

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

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

Yep, I came to the same conclusion https://twitter.com/jaffathecake/status/1296423819238944768

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

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

I don't think so. Whereas an if statement is easy to read https://twitter.com/jaffathecake/status/1296423819238944768
Post reply on HN