[Python-ideas] Consider adding clip or clamp function to math
Random832
random832 at fastmail.com
Thu Aug 4 13:58:55 EDT 2016
On Thu, Aug 4, 2016, at 12:24, Alexander Belopolsky wrote:
> On Thu, Aug 4, 2016 at 11:48 AM, Victor Stinner
> <victor.stinner at gmail.com> wrote:
> > When I really need such function, I define it like this:
> >
> > def clamp(min_val, value, max_val): return min(max(min_val, value), max_val)
>
> and your colleague next door defines it like this:
>
> def clamp(min_val, value, max_val): return min(max_val, max(value ,
> min_val))
Ideally min and max should themselves be defined in a way that makes
that not an issue (or perhaps only an issue for different-signed zero
values)
> and a third party library ships
>
> def clamp(min_val, value, max_val): return max(min(max_val, value),
> min_val)
That one is more of an issue, though AIUI only so when min_val >
max_val.
More information about the Python-ideas
mailing list