Math.min(Math.max(num, min), max)
twitter.com
Math.min(Math.max(num, min), max)
1–10 of 291 posts
Re: Math.min(Math.max(num, min), max)
#2 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)
#3The 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)
#4 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)
#5The 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...
function clamp(num, min, max) {
if (num > max)
return max;
if (num Re: Math.min(Math.max(num, min), max)
#6It 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)
#7But 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)
#8The 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)
#9The 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)
#10> 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)