Live data from Hacker News

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

twitter.com

161–170 of 291 posts

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

#161

Earlier quoted context omitted.

Not sure what you mean. If I want to clamp 1 between 2 and 0, the most reasonable answer is 1, which is correctly returned by this code.

He's saying that, if you have explicitly defined a max and min such that max The array implementation sidesteps this by not semantically defining a max and min, instead sorting three arbitrary numbers.

Why do you prefer to leave undefined behavior?

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

#162
post #28

I find that the fact that the functions min and max have the same name as the variables min and max increases cognitive load which makes it harder to think about it. I find the following easier to read : Math.min(Math.max(num, lower_bound), upper_bound)

That doesn't really help. "Max" to enforce a "lower bound" is briefly halting.

I think the reason it's weird is that we might intuitively think of the "enforce a lower bound" function as taking two named arguments (lowerBound and inputValue) and the order of those two arguments mattering.

But of course, it turns out that the order of the arguments doesn't matter: applying a lowerBound of 5 to an inputValue of 100 turns out to be the exact same thing as applying a lowerBound of 100 to an inputValue of 5.

We know that the order of arguments doesn't matter for the Math.max function, so I think that's where the moment of incredulity comes from.

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

#163

Is this a problem that comes up often? What kind of situation would make you want to calculate this?

It's common in computer graphics and user interface programming. You want to move your character around a 2D grid, but you don't want either of its x and y coordinates to ever move outside the bounds of the 2D grid. Or you want a web page with an article section that is 50% of the browser width, but never narrower than 300px and never wider than 800px.

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

#164
post #4

Very coincidental I see this post almost immediately after writing the same code: 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.

If fastest poll rate > slowest poll rate, I think you've got them the wrong way around (or is that the joke?).

FASTEST_POLL_RATE is 1000hz, SLOWEST_POLL_RATE is 10hz. Thus, FASTEST_POLL_RATE is a smaller number on a per-second basis.

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

#165

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.

It depends on what space you're working on (e.g. the https://en.wikipedia.org/wiki/Extended_real_number_line define an order on the real field union {-∞, +∞}).

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

#168
post #145
post #109

Earlier quoted context omitted.

this doesn't work for NaN, e.g {0d, Double.NaN, 1d} returns 1d.

So I tried a thing in python3. >>> max(float('nan'), 0) nan >>> max(0, float('nan')) 0 Numpy works though >>> np.maximum(float('nan'), 0) nan >>> np.maximum(0, float('nan')) nan Edit: Fixed numpy example.

And for this particular use case, numpy has .clip(), that takes:

    np.clip(ndarray, lower_bound, upper_bound)
And handles NaNs correctly.

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

#170
post #164

Earlier quoted context omitted.

If fastest poll rate > slowest poll rate, I think you've got them the wrong way around (or is that the joke?).

FASTEST_POLL_RATE is 1000hz, SLOWEST_POLL_RATE is 10hz. Thus, FASTEST_POLL_RATE is a smaller number on a per-second basis.

Ah, I see - for clarity I'd rename them FASTEST_POLL_RATE -> SHORTEST_POLL_PERIOD or store them in Hz rather than seconds, so everything was 1/ in that little snippet. Thanks for clearing up my confusion :)
Post reply on HN