def clip(val, lower, upper):
    """
    Given an interval, the value outside the interval is clipped to the interval edges.
    :param val: The value to clip.
    :param lower: Minimum value.
    :param upper: Maximum value.
    :return: Clipped value.
    """
    val = max(lower, val, key=lambda x: float(x))
    val = min(upper, val, key=lambda x: float(x))
    return val