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.
Math.min(Math.max(num, min), max)
161–170 of 291 posts
Re: Math.min(Math.max(num, min), max)
#162I 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.
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)
#163Is this a problem that comes up often? What kind of situation would make you want to calculate this?
Re: Math.min(Math.max(num, min), max)
#164Very 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?).
Re: Math.min(Math.max(num, min), max)
#165Earlier 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)
#166[min, num, max].sort()[1]
Re: Math.min(Math.max(num, min), max)
#167 num max ? max :
numRe: Math.min(Math.max(num, min), max)
#168Earlier 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.
np.clip(ndarray, lower_bound, upper_bound)
And handles NaNs correctly.Re: Math.min(Math.max(num, min), max)
#169Re: Math.min(Math.max(num, min), max)
#170Earlier 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.