How to Clamp Floating Numbers in Python?

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 if it is less than the min value, and the higher value if it is greater than the max value.

Example:

Input:

Given Numbers : 25 150 30

Output:

30

Explanation:

As 30 is between the 25 and 150

Clamping Floating Numbers in Python

The clamping of floating numbers in python can be done in the below ways:

Method #1: Using user-defined Function

In Python, there is no built-in clamping function. The clamping function can be defined as follows:

def clamp(number, min, max):
    return min if number < min else max if number> max else number

Approach:

  • Create a function say clamping which accepts a number, minimum value, maximum value passed
  • as the arguments to it.
  • Inside the function, check if the given number is less than the minimum value if it is true then return the minimum value
  • Else check if the given number is more than maximum value if it is true then return maximum value
  • Else return given number
  • Pass the number, minimum value, maximum value as the arguments to the above created user-defined clamping() function and print the result
  • Similarly, check out for the other numbers.
  • The Exit of the Program.

Below is the implementation:

# Create a function say clamping which accepts a number, minimum value, maximum value passed
# as the arguments 
def clamping(gvn_number, minimumval, maximumval):
    # Check if the given number is less than minimumval if it is true then return minimumval
    # Else check if the given number is more than maximumval if it is true then return maximumval
    # Else return given number
    return minimumval if gvn_number < minimumval else maximumval if gvn_number > maximumval else gvn_number
# Pass the number, minimum value, maximum value as the arguments to the above created clamping() function
# and print the result
print(clamping(1.5, 2, 4))
# Similarly check out for the other numbers
print(clamping(1.23, 1.15, 1.31))
print(clamping(2.35, 1.10, 1.25))

Output:

2
1.23
1.25

Method #2: Using numpy.clip() Function

Also, we can we the numpy.clip() function to clamp the numbers.

Syntax:

numpy.clip(number, min, max)

Approach:

  • Import numpy module using the import keyword
  • Pass some random number, minimum value, maximum value as the arguments to the clip() function of the numpy module to clip/clamp the given number.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy
# Pass some random number, minimum value, maximum value as the arguments to the clip() function
# of the numpy module to clip/clamp the given number.
numpy.clip(3.5, 2, 4)

Output:

3.5

Similarly, check for the other numbers

# Import numpy module using the import keyword
import numpy
# Pass some random number, minimum value, maximum value as the arguments to the clip() function
# of the numpy module to clip/clamp the given number.
numpy.clip(1.23, 1.15, 1.31)

Output:

1.23

Method #3: Using PyTorch clamp() Function

We can clamp the input element in the given range using the torch.clamp() method from the PyTorch library.

Syntax:

torch.clamp(number, min, max, out=None)

Below is the implementation:

import torch

print(ex1 = torch.clamp(0.5, min = 5, max = 10))
print(ex2 = torch.clamp(0.27, min = 0.11, max = 0.35))

Output:

5
0.23 
0.27