Live data from Hacker News

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

twitter.com

91–100 of 291 posts

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

#91

Earlier quoted context omitted.

I’ve been programming for living since 2000, but I don’t think that’s relevant. No reason not to use what’s available in standard libraries of whatever language you’re writing. For example, C++ on AMD64 is very likely to compile std::clamp into 2 instructions, minsd and maxsd. I’m not so sure about nested ternaries mentioned elsewhere in the comments.

There are very good reasons to avoid std:: stuff. And if you don't already know that in your soul, I will appear to be a genuine crackpot, and the reasons not to use std::* will still exist.

I think you’re overgeneralizing here.

I’m aware some parts of C++ standard library are outright horrible, like iostream and I/O in general. Other parts are questionable, like date & time, locales, and futures.

Meanwhile, other parts of the same standard library are actually OK (most collections, threading, synchronization, atomics, smart pointers, initializer lists). And other parts are awesome, like most of the stuff from header.

Apparently, one of the C++ design goals was to not pay for features which aren’t used. Selectively ignoring stuff from the standard library doesn’t have much downsides.

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

#93

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.

> in desktop edition of the runtime

huh?

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

#94

This is a great example of how haskell makes these things obvious ;) clamped = num `max` min' `min` max'

no idea what's going on there, I know some of those variables are actually functions but the whole thing is unreadable unless you have experience in haskell imo

The backticks turn regular functions into left-associative operators of the highest priority (by default).

So

    a `foo` b `bar` c
is

    (bar (foo a b) c)

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

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

> Next challenge: teach the optimizer to make that almost as fast as the min/max way ;-) I did exactly this for my PhD! https://chrisseaton.com/phd/

For those reading, this work went into TruffleRuby, which implements Ruby on top of Truffle/GraalVM.

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

#96
post #46

Earlier quoted context omitted.

Speaking only to JS is there any reason to write it any other way outside of being clever or as a lambda for singular use? I definitely prefer this version. (Assuming any necessary runtime checks are included for a given project)

those extra newline characters slow down the page load :)

You’re joking right

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

#97

Always 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

I often see [a, b, c].sort[1] which I think is very neat.

Yes but its too many operations for such a simple problem. No need to overwork the system when two conditionals can solve the problem.

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

#98
It is simpler if you use the notation [x]_a^b (i.e. with a subscript and a superscript b) to mean x, clipped to the range a to b, and skip writing +/- infinity if you don't intend clipping on one side.

Then you get a bunch of obvious identities like [x]^b = min(x, b) = [b]^x (x capped by b is the same as the smaller of x and b which is the same as b capped by x), [x]_a^b = [b]_a^x, and [x]_a^b = [[x]_a]^b. Putting these together you get [x]_a^b = [[x]_a]^b = min(max(x, a), b). But honestly it's just easier to stick to the notation most of the time.

A better write-up, for everyone who doesn't like reading new math notations inline: https://imgur.com/gallery/593QEow (Imgur link with white background) https://quicklatex.com/cache3/71/ql_46c49ac709b3789482d0736d... (Original link - renders badly in Chrome due to PNG transparency)

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

#99
post #18
post #15

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

"lexicographically" is a better descriptor than "alphabetically", since it's sorting by UTF-8 code point value.
Post reply on HN