In APL: lower⌈ upper⌊ numbers See a stream of random numbers flowing from right to left. See the higher ones being pushed down ⌊ to the upper bound, and the lower ones being pushed up ⌈ to the lower bound, and the middle ones flowing through both guards unchanged.
Math.min(Math.max(num, min), max)
271–280 of 291 posts
Re: Math.min(Math.max(num, min), max)
#272Earlier quoted context omitted.
Beware that in JavaScript, the language that this tweet is about, the sort function sorts alphabetically by default. [100, 10, 11].sort()[1] === 100
Wait, really? BRB, just checking some code…
Re: Math.min(Math.max(num, min), max)
#273 5 9 7 min max .
This prints 7, as it's between 5 (lower bound) and 9 (upper bound).Defining a clamp "function" looks like this:
: clamp ( min max n -- clamped_n )
min max ;Re: Math.min(Math.max(num, min), max)
#274Earlier quoted context omitted.
Easy to remember, but may take some time to grasp: Arrays.sort( {lower_bound, num, upper_bound} )[1]; Next challenge: teach the optimizer to make that almost as fast as the min/max way ;-) (You can’t reduce it to the min/max call because it also works if you accidentally pass a lower bound that’s larger than the upper bound. Worst-case, the above takes 3 comparisons, unless at least two of the inputs are constants)
Beware that in JavaScript, the language that this tweet is about, the sort function sorts alphabetically by default. [100, 10, 11].sort()[1] === 100
Re: Math.min(Math.max(num, min), max)
#275In 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.
Great. But I have been programming since about 1985.
> Fout bij het maken van de databaseconnectie
Re: Math.min(Math.max(num, min), max)
#276Earlier quoted context omitted.
Branching is the expensive part, not comparisons.
This is 3 branches. The Math method ends up with 4. This is one of those cases where I think it is much more readable to just write the code than to puzzle over what Math.min(Math.max(min, num), max); might be doing. if (num max) return max; return num; That's how I'd write it. May not be super terse, but anyone that stumbles on this will know precisely what's happening without needing to take a few seconds to puzzle…
In Javascript in my browser (Safari) when these methods get called enough to get compiled using the most aggressive stage of the JIT, they end up essentially the same speed. On my laptop either one runs about 145 million times per second on one CPU core.
I wouldn’t be surprised if a C compiler ended up making this function significantly faster, but I haven’t tested it.
Re: Math.min(Math.max(num, min), max)
#277Earlier quoted context omitted.
Does NaN have an "order" in the set of reals or integers or whatever? I would have no idea what to expect from `min(NaN, x)` or max same. But is it specified by an IEEE standard or something?
> Does NaN have an "order" in the set of reals or integers or whatever? By definition , something that is not a number (real, integer, etc.) cannot be compared to something that is a number.
Re: Math.min(Math.max(num, min), max)
#278Earlier quoted context omitted.
this doesn't work for NaN, e.g {0d, Double.NaN, 1d} returns 1d.
Having a NaN at that point feels like a bug anyway, the solution is probably to check the arguments and throw an exception if NaN is provided (or use an input type that doesn't allow invalid values)
Re: Math.min(Math.max(num, min), max)
#279Earlier quoted context omitted.
Easy to remember, but may take some time to grasp: Arrays.sort( {lower_bound, num, upper_bound} )[1]; Next challenge: teach the optimizer to make that almost as fast as the min/max way ;-) (You can’t reduce it to the min/max call because it also works if you accidentally pass a lower bound that’s larger than the upper bound. Worst-case, the above takes 3 comparisons, unless at least two of the inputs are constants)
Beware that in JavaScript, the language that this tweet is about, the sort function sorts alphabetically by default. [100, 10, 11].sort()[1] === 100
Re: Math.min(Math.max(num, min), max)
#280In Factor: : clamp ( x min max -- y ) [ max ] dip min ; inline https://docs.factorcode.org/content/word-clamp,math.order.ht...
So this pops the max value off the stack, applies the quoted `max` word to the x and min stack values, then pops the max value back on the stack and applies the `min` word to the result and the max.