def limit(min, max, v):
    """limits the range of v between min and max
    """
    if min > max:
        raise RuntimeError('Min value (%f) larger than max (%f)' % (min, max))
    if v < min:
        return min
    elif v < max:
        return v
    else:
        return max
