Remix.run Logo
IshKebab 12 hours ago

> clamp comes down to sorting the three numbers and returning the middle one

That is a really weird definition of clamp. I would expect to consistently get either min, max, or an error.

I bet Rust doesn't use your weird idea... Yep. Fully sane implementation:

https://docs.rs/num/latest/num/fn.clamp.html

tobr 8 hours ago | parent [-]

It’s not a definition, it’s an implementation. A definition would be something like ”constrain a number to an interval defined by two other numbers”. That happens to be identical to sorting and picking the middle value. The only difference vs OP’s clamp is that it gives a consistent and predictable result for ”inverted” intervals where min > max, instead of arbitrarily deciding how to collapse the interval.

IshKebab 4 hours ago | parent [-]

It is a definition. You've defined the behaviour when min>max and you've chosen a weird way to define it.