This was proposed about four years ago, Here is a link to the first post 
in the thread:

https://mail.python.org/pipermail/python-ideas/2016-July/041262.html

Discussion spilled over into the following month, here's the first post 
following:

https://mail.python.org/pipermail/python-ideas/2016-August/041276.html

As I recall, there was some positive support but it ran out of steam 
because nobody could agree on how to handle NANs even though the 
IEEE-754 standard tells us how to handle them *wink*

See my responses at the time re NANs here:

https://mail.python.org/pipermail/python-ideas/2016-August/041439.html

https://mail.python.org/pipermail/python-ideas/2016-August/041400.html

https://mail.python.org/pipermail/python-ideas/2016-August/041396.html

Bottom line is that passing a NAN as the lower or upper bound should 
treat it as equivalent to "unbounded", that is, equivalent to ±∞.

The beauty of that is that it can be implemented without explicitly 
testing for NANs, which involves unnecessary conversions to float, and 
may even raise an exception.

Here is the version I use:

    
    def clamp(value, lower, upper):
        """Clamp value to the closed interval lower...upper.

        The limits lower and upper can be set to None to
        mean -∞ and +∞ respectively.
        """
        if not (lower is None or upper is None):
            if lower > upper:
                raise ValueError('lower must be <= to upper')
        if lower == upper is not None:
            return lower
        if lower is not None and value < lower:
            value = lower
        elif upper is not None and value > upper:
            value = upper
        return value


Features:

* uses None as a convenient alias for unbounded;

* treats NANs according to the standard;

* requires no explicit conversion to float or testing for NANs;

* so this will work with Fractions and Decimals.


By the way, using "minmax" as the name would be inappropriate as that 
typically has other meanings, either to return the minimum and maximum 
in a single call, or in the sense of minimizing the maximum value of 
some function or process.



-- 
Steven
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/NIQOL3DG3F3PIH234R24EVRMJ7LZYSZL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to