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

Victor Stinner victor.stinner at gmail.com
Thu Aug 4 15:46:05 EDT 2016


Le 4 août 2016 19:59, "Random832" <random832 at fastmail.com> a écrit :
> > 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)

There is a generic sum() and a specific math.fsum() function which is more
accurate to sum a list of float.

Maybe before starting to talk about clamp(), we should define new
math.fmin() and math.fmax() functions?

A suggest to start to write a short PEP as the math.is_close() PEP since
there are subtle issues like NaN (float but also Decimal!) and combinations
of numerical types (int, float, complex, Decimal, Fraction, numpy scalars
like float16, ...).

Maybe a PEP is not needed, I didn't read carefully the thread to check if
there is a consensus or not.

I dislike the idea of modifying min() and max() to add special cases for
float NaN and decimal NaN.

Which type do you expect for fmax(int, int)? Should it be int or float?

Should fmax(Decimal, float) raise an error, return a float or return a
Decimal?

Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160804/6e249e34/attachment.html>


More information about the Python-ideas mailing list