Is there a built-in function for this in Python 2.6?
Something like:
clamp(myValue, min, max)
Use the int Function to Truncate a Float in Python The built-in int() function takes a float and converts it to an integer, thereby truncating a float value by removing its decimal places.
Python – PyTorch clamp() method inp: This is input tensor. min: This is a number and specifies the lower-bound of the range to which input to be clamped. max: This is a number and specifies the upper-bound of the range to which input to be clamped. out: The output tensor.
clip() function is used to Clip (limit) the values in an array. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
Numpy's clip
function will do this.
>>> import numpy >>> numpy.clip(10,0,3) 3 >>> numpy.clip(-4,0,3) 0 >>> numpy.clip(2,0,3) 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With