{"id":26548,"date":"2022-05-10T22:20:05","date_gmt":"2022-05-10T16:50:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=26548"},"modified":"2022-05-10T22:20:05","modified_gmt":"2022-05-10T16:50:05","slug":"how-to-clamp-floating-numbers-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-clamp-floating-numbers-in-python\/","title":{"rendered":"How to Clamp Floating Numbers in Python?"},"content":{"rendered":"
In this article, let us see how to clamp or clip floating-point numbers in Python.<\/p>\n
What do you mean by clamping a Number?<\/strong><\/p>\n The clamp function in python limits a number between two numbers. If a number is clamped, it retains its value if it falls inside the specified range. It takes the lower value if it is less than the min value, and the higher value if it is greater than the max value.<\/p>\n Example:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n The clamping of floating numbers in python can be done in the below ways:<\/p>\n In Python, there is no built-in clamping function. The clamping function can be defined as follows:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Also, we can we the numpy.clip() function to clamp the numbers.<\/p>\n Syntax:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, check for the other numbers<\/p>\n Output:<\/strong><\/p>\n We can clamp the input element in the given range using the torch.clamp() method from the PyTorch library.<\/p>\n Syntax:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In this article, let us see how to clamp or clip floating-point numbers in Python. What do you mean by clamping a Number? The clamp function in python limits a number between two numbers. If a number is clamped, it retains its value if it falls inside the specified range. It takes the lower value …<\/p>\nGiven Numbers : 25 150 30<\/pre>\n
30<\/pre>\n
As 30 is between the 25 and 150<\/pre>\n
Clamping Floating Numbers in Python<\/h2>\n
\n
Method #1: Using user-defined Function<\/strong><\/h3>\n
def clamp(number, min, max):\r\n \u00a0\u00a0\u00a0return min if number < min else max if number> max else number<\/pre>\n<\/div>\n
\n
# Create a function say clamping which accepts a number, minimum value, maximum value passed\r\n# as the arguments \r\ndef clamping(gvn_number, minimumval, maximumval):\r\n # Check if the given number is less than minimumval if it is true then return minimumval\r\n # Else check if the given number is more than maximumval if it is true then return maximumval\r\n # Else return given number\r\n return minimumval if gvn_number < minimumval else maximumval if gvn_number > maximumval else gvn_number\r\n# Pass the number, minimum value, maximum value as the arguments to the above created clamping() function\r\n# and print the result\r\nprint(clamping(1.5, 2, 4))\r\n# Similarly check out for the other numbers\r\nprint(clamping(1.23, 1.15, 1.31))\r\nprint(clamping(2.35, 1.10, 1.25))<\/pre>\n
2\r\n1.23\r\n1.25<\/pre>\n
Method #2: Using numpy.clip() Function<\/h3>\n
numpy.clip(number, min, max)<\/pre>\n
\n
# Import numpy module using the import keyword\r\nimport numpy\r\n# Pass some random number, minimum value, maximum value as the arguments to the clip() function\r\n# of the numpy module to clip\/clamp the given number.\r\nnumpy.clip(3.5, 2, 4)<\/pre>\n
3.5<\/pre>\n
# Import numpy module using the import keyword\r\nimport numpy\r\n# Pass some random number, minimum value, maximum value as the arguments to the clip() function\r\n# of the numpy module to clip\/clamp the given number.\r\nnumpy.clip(1.23, 1.15, 1.31)<\/pre>\n
1.23<\/pre>\n
Method #3: Using PyTorch clamp() Function<\/h3>\n
torch.clamp(number, min, max, out=None)<\/pre>\n
import torch\r\n\r\nprint(ex1 = torch.clamp(0.5, min = 5, max = 10))\r\nprint(ex2 = torch.clamp(0.27, min = 0.11, max = 0.35))<\/pre>\n
5\r\n0.23 \r\n0.27<\/pre>\n