classification
Title: Add a `clamp` function to the `math` module
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Add clamp() function to builtins
View: 36788
Assigned To: Nosy List: SFM61319, mark.dickinson, rhettinger, stutzbach, xtreak
Priority: normal Keywords:

Created on 2020-07-27 12:28 by SFM61319, last changed 2020-07-28 07:32 by mark.dickinson. This issue is now closed.

Messages (6)
msg374373 - (view) Author: Avinash Maddikonda (SFM61319) * Date: 2020-07-27 12:28
Add a `clamp` function to the `math` module which does something like this:  
  
```py
def clamp(value=0.5, minimum=0, maximum=1):
    """Clamps the *value* between the *minimum* and *maximum* and returns it..

<And some extra explanatory documentation with examples>
"""

    return max(minimum, min(value, maximum))
```
  
Because even `C++` has built-in clamp function (`std::clamp`) (which can be used using `#include <math.h>`)
msg374376 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-07-27 13:18
This seems to be a duplicate of https://bugs.python.org/issue36788
msg374388 - (view) Author: Avinash Maddikonda (SFM61319) * Date: 2020-07-27 15:37
Oh, I didn't know that. But I was thinking of adding it to the math module and not as a built-in method. I mean, it is definitely more useful than copysign, right?
msg374391 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-07-27 15:53
I will leave it to math module experts to keep it open or close it. There wasn't strong consensus on adding it math module either without a discussion in python-ideas https://bugs.python.org/msg358789.
msg374393 - (view) Author: Avinash Maddikonda (SFM61319) * Date: 2020-07-27 16:00
So should I delete/reject this now? Because I think it would be more useful than functions like copysign (I mean copysign can be written easily without confusion, but clamp is confusing sometimes as the max function takes minimum and the min of value and the maximum, and yet we have a copysign function but not a clamp built-in or a math module function).

(I'll delete/reject if you guys want me to, np)
msg374396 - (view) Author: Avinash Maddikonda (SFM61319) * Date: 2020-07-27 16:23
Okay, I'm rejecting this issue. But please let me know/contribute if anyone is going to add a clamp function as a built-in method or a math module function.
History
Date User Action Args
2020-07-28 07:32:49mark.dickinsonsetsuperseder: Add clamp() function to builtins
resolution: rejected -> duplicate
2020-07-27 16:23:46SFM61319setstatus: open -> closed
resolution: rejected
stage: resolved
2020-07-27 16:23:28SFM61319setmessages: + msg374396
2020-07-27 16:00:01SFM61319setmessages: + msg374393
2020-07-27 15:53:25xtreaksetnosy: + rhettinger, mark.dickinson, stutzbach
messages: + msg374391
2020-07-27 15:37:53SFM61319setmessages: + msg374388
2020-07-27 13:18:49xtreaksetnosy: + xtreak
messages: + msg374376
2020-07-27 12:28:33SFM61319create