from typing import Union


def constrain(value: Union[int, float], max_val: Union[float, int], min_val: Union[float, int]=0):
    """
    Constrain given value in given boundaries.

    :param value: value to be constrained
    :param max_val: value cannot be more than this
    :param min_val: returned value won't be lower than this
    :return: value constrained within given range
    """
    if max_val < min_val:
        max_val, min_val = min_val, max_val
    return max(min_val, min(value, max_val))