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/
Math.min(Math.max(num, min), max)
121–130 of 291 posts
Re: Math.min(Math.max(num, min), max)
#122Earlier quoted context omitted.
I think this was posted purely for the limerick quality.
There was a young coder whose hacks His manager often claimed lacked The requisite clarity For to clamp vars would he: Math.min(Math.max(number, min), max);
@jaffathecake had a problem of truncation
and posted to Twitter his calculation.
The gist of his attack
was min( max( num, min ), max)
yet refused to add any annotation.Re: Math.min(Math.max(num, min), max)
#123Earlier quoted context omitted.
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.
Are you saying a C++ developer shouldn't use the standard library?
Re: Math.min(Math.max(num, min), max)
#124Re: Math.min(Math.max(num, min), max)
#125Earlier quoted context omitted.
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.
Re: Math.min(Math.max(num, min), max)
#126Is that supposed to be difficult?
Not trying to be snarky, I'm honestly surprised, do people actually struggle with this? Googlers in particular?
Re: Math.min(Math.max(num, min), max)
#127Earlier quoted context omitted.
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.
Are you saying a C++ developer shouldn't use the standard library?
But as a sibling commenter notes... this isn't relevant for min/max.
Re: Math.min(Math.max(num, min), max)
#128 # d0 = num, d1 = low, d2 = high, d3 = scratch
sub.l d1, d0
subx.l d3, d3
or.l d3, d0
addx.l d1, d0 # d0 = max(num,low)
sub.l d2, d0
subx.l d3, d3
and.l d3, d0
add.l d2, d0 # d0 = min(max(num,low),high)
Adapted from "Superoptimizer -- A Look at the Smallest Program" by Henry Massalin [1].[1] https://web.stanford.edu/class/cs343/resources/superoptimize...
Re: Math.min(Math.max(num, min), max)
#129Re: Math.min(Math.max(num, min), max)
#130Luckily, this is a solved problem for go. func helper(a float64, c chan float64){ time.Sleep(time.Duration(a) * time.Second) c