`Math.clamp` strawman

I believe the most important feature from the Math extensions proposal (last presented in 2016) is the Math.clamp function but the proposal as a whole has since gone quiet. Perhaps it was its amibition for features had eventually caused a roadblock in development.

But regardless, I wanted to take out only the Math.clamp function from the proposal and improve upon it in its own proposal. I'm not sure how the process of finding a champion, or for that matter, getting feedback in the first place goes, but I'll just put this out there.

Proposal: GitHub - Richienb/proposal-math-clamp: ECMAScript proposal and reference implementation for `Math.clamp`.


Could also support BigInt from the get go, if this other proposal advances.

You might want to reach out to @jschoi and see if they would be interested in championing this proposal. Seems like it would be easy to argue that it is motivated.

I am interested in championing (the explainer linked above looks pretty good), but I probably would not have the bandwidth to do so until after at least BigInt Math resolves.


FWIW, it is mathematically the same as a median function with 3 numbers (in any order), so this thread may be related: typedarray.p.[median() | lowerQuartile() | upperQuartile()].

1 Like

They are not the same. Median of 3 must do 3 comparisons, clamp only does 2. Clamp doesn't bother checking whether low <= high, you're supposed to "sort" the boundary arguments. When low > high, clamp returns low (or high, may depend on implementation), not the median.

1 Like

Well, that’s why I said they are mathematically the same. Personally I don’t think returning low or high when low > high is a good idea though.