Live data from Hacker News

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

twitter.com

271–280 of 291 posts

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

#271

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.

[deleted]

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

#272

Earlier 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…

Yep, the sort method converts values to strings before comparing. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

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

#274
post #75

Earlier 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

Most popular programming language folks. You give it 3 numbers and it treats them as 3 strings of characters...

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

#275

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.

Great. But I have been programming since about 1985.

Your database on https://mecoffee.nl seems to be down

> Fout bij het maken van de databaseconnectie

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

#276

Earlier 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…

What you want is something that compiles to conditional moves. A good compiler should compile your proposed version to the same machine code as the max & min method. If for one reason or another it compiles to branches, it’ll end up being slower.

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)

#277

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

typeof NaN

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

#278
post #109

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

That would depend on what you do with the NaNs. For instance I have been using them extensively in time series data representation to denote a specific entry has no value - think of Saturday and stock/forex markets.

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

#279
post #75

Earlier 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

I was confused why the author claims this doesn't work, then I saw JavaScript... ah

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

#280
post #87

In Factor: : clamp ( x min max -- y ) [ max ] dip min ; inline https://docs.factorcode.org/content/word-clamp,math.order.ht...

For casual readers, `dip` pops the top of the stack, executes a quotation, then pushes the top of the stack back on.

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.

Post reply on HN