Live data from Hacker News

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

twitter.com

121–130 of 291 posts

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

#121
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/

Wait, how is that not trivial?

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

#122
post #68
post #30

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

#123
post #79

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

Not the person you are replying to, but "a lot of the standard library is horrifying and should never be touched" is pretty standard advice for C++. Mind, I don't think this particular function belongs to that set.

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

#125
post #18

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

It sorts by UTF-16 code unit.

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

#127
post #79

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

Many performance-critical C++ programmers treat std with suspicion. One thing to keep in mind is that the interface is standard, but the implementations are not, and can foil you on cross-platform development. Another is that you might not need everything that a std container provides, and you can get away with a streamlined data structure that doesn't support those unnecessary operations.

But as a sibling commenter notes... this isn't relevant for min/max.

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

#128
68K assembly, no branches:

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

#129
post #121

Earlier quoted context omitted.

> 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/

Wait, how is that not trivial?

It's not just a hardcoded optimization for that construct.
Post reply on HN