/usr/lib/python2.7/dist-packages/boltons/mathutils.py is in python-boltons 17.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | """This module provides useful math functions on top of Python's
built-in :mod:`math` module.
"""
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*. Integers, floats, and other comparable types can be
mixed.
>>> clamp(1.0, 0, 5)
1.0
>>> clamp(-1.0, 0, 5)
0
>>> clamp(101.0, 0, 5)
5
Similar to `numpy's clip`_ function.
.. _numpy's clip: http://docs.scipy.org/doc/numpy/reference/generated/numpy.clip.html
"""
if upper < lower:
raise ValueError('expected upper bound (%r) >= lower bound (%r)'
% (upper, lower))
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
*x*.
Args:
x (int or float): Number to be tested.
options (iterable): Optional iterable of arbitrary numbers
(ints or floats).
>>> VALID_CABLE_CSA = [1.5, 2.5, 4, 6, 10, 25, 35, 50]
>>> ceil(3.5, options=VALID_CABLE_CSA)
4
>>> ceil(4, options=VALID_CABLE_CSA)
4
"""
if options is None:
return _ceil(x)
options = sorted(options)
i = bisect.bisect_left(options, x)
if i == len(options):
raise ValueError("no ceil options greater than or equal to: %r" % x)
return options[i]
def floor(x, options=None):
"""Return the floor of *x*. If *options* is set, return the largest
integer or float from *options* that is less than or equal to
*x*.
Args:
x (int or float): Number to be tested.
options (iterable): Optional iterable of arbitrary numbers
(ints or floats).
>>> VALID_CABLE_CSA = [1.5, 2.5, 4, 6, 10, 25, 35, 50]
>>> floor(3.5, options=VALID_CABLE_CSA)
2.5
>>> floor(2.5, options=VALID_CABLE_CSA)
2.5
"""
if options is None:
return _floor(x)
options = sorted(options)
i = bisect.bisect_right(options, x)
if not i:
raise ValueError("no floor options less than or equal to: %r" % x)
return options[i - 1]
|