
Aug. 5, 2016
3:09 a.m.
Steven D'Aprano writes:
clamp(x, y, z) --> NaN if z < y
That's a clear error, and it should raise immediately. I see no advantage to returning NAN in this case.
Think about why you're clamping. It's unlikely to be used just once, for a single calculation. You're likely to be clamping a whole series of values, with a fixed lower and upper bounds.
"Likely" isn't a good enough reason: x = [clamp(x[t], f(t), g(t)) for t in range(1_000_000)] is perfectly plausible code. The "for which case is it easier to write a wrapper" argument applies here, I think.