def constrain_to_range(s, min_val, max_val):
    """
    Make sure that a value lies in the given (closed) range.

    :param s: Value to check.
    :param min_val: Lower boundary of the interval.
    :param max_val: Upper boundary of the interval.
    :return: Point closest to the input value which lies in the given range.
    :rtype: float
    """
    return max(min(s, max_val), min_val)