[Python-ideas] Consider adding clip or clamp function to math
Stephen J. Turnbull
turnbull.stephen.fw at u.tsukuba.ac.jp
Thu Aug 4 23:09:09 EDT 2016
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.
More information about the Python-ideas
mailing list