
On 3 August 2016 at 15:36, Rob Cliffe <rob.cliffe@btinternet.com> wrote:
If x < y, you might argue that the result should be y. But consider clamp(x, 2, 1). You're asking it to limit x to a value not less than 2 and not greater than 1. There's no such number, so arguably the result should be NaN.
I think clamp(x,2,1) should raise ValueError. It's asking for something impossible.
Agreed.
If you accept that, then clamp(x, y, NaN) should be NaN in all cases, since we don't know that the upper bound isn't less than the lower bound.
+0.8. Returning y when x<y might suit some applications better, but "resist the temptation to guess".
clamp(val, lo, hi) should raise ValueError if either lo or hi is NaN, for the same reason (lo < hi doesn't hold, in this case because the values are incomparable). Paul