From bc542e3ddb6717acca775dc4003fec54702bf086 Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Thu, 4 Aug 2016 23:30:40 -0700 Subject: [PATCH] add mathutils.clamp --- boltons/mathutils.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/boltons/mathutils.py b/boltons/mathutils.py index 0158822..b39312c 100644 --- a/boltons/mathutils.py +++ b/boltons/mathutils.py @@ -5,6 +5,28 @@ from math import ceil as _ceil, floor as _floor import bisect +def clamp(x, lower=None, upper=None): + """Limit a value to a given range. + + Args: + x (int or float): Number to be clamped. + lower (int or float): Minimum value for x. + upper (int or float): Maximum value for x. + + The returned value is guaranteed to be between *lower* and + *upper*. Arguments are not type-checked, but ints and floats are + supported. + + >>> clamp(1.0, 0, 5) + 1.0 + >>> clamp(-1.0, 0, 5) + 0 + >>> clamp(101.0, 0, 5) + 5 + """ + return min(max(x, lower), upper) + + def ceil(x, options=None): """Return the ceiling of *x*. If *options* is set, return the smallest integer or float from *options* that is greater than or equal to