25.clamp(5, 10)
=> 10
6.clamp(5, 10)
=> 6
1.clamp(5, 10)
=> 5Math.min(Math.max(num, min), max)
11–20 of 291 posts
Re: Math.min(Math.max(num, min), max)
#12> 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 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)
#13Stage 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)
#14C++/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)
#15 [min, num, max].sort()[1]Re: Math.min(Math.max(num, min), max)
#16The 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
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)
#17Stage 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)
#18[min, num, max].sort()[1]
https://stackoverflow.com/questions/21019902/why-cant-javasc...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Re: Math.min(Math.max(num, min), max)
#19Always 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
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[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...