[Python-ideas] Consider adding clip or clamp function to math

Ian Kelly ian.g.kelly at gmail.com
Sun Jul 31 09:14:03 EDT 2016

On Sun, Jul 31, 2016 at 7:10 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Sun, Jul 31, 2016 at 2:19 AM, Steven D'Aprano <steve at pearwood.info> wrote:
>> I'm not sure how your version would work with NANs, and I haven't
>> bothered to try it to find out, but I like this version:
> Answer:
> py> min(10, float('nan'))
> 10

Ah, but the original passed the value before the boundary.

py> min(float('nan'), 10)
py> max(_, 0)

So it works, but it's somewhat fragile because it depends on the order
of the arguments to min and max.

More information about the Python-ideas mailing list