Clamping floating numbers in Python?
An answer to this question on Stack Overflow.
Question
Is there a built-in function for this in Python 2.6?
Something like:
clamp(myValue, min, max)
Answer
Numpy's [clip][1] function will do this.
>>> import numpy
>>> numpy.clip(10,0,3)
3
>>> numpy.clip(-4,0,3)
0
>>> numpy.clip(2,0,3)
2
[1]: http://docs.scipy.org/doc/numpy/reference/generated/numpy.clip.html