def clamp(a, x, b):
    """
    Ensures x lies in the closed interval [a, b]
    :param a:
    :param x:
    :param b:
    :return:
    """
    if x > a:
        if x < b:
            return x
        else:
            return b
    else:
        return a