def normalize(num, lower=<NUM_LIT:0>, upper=<NUM_LIT>, b=False):
if lower >= upper:<EOL><INDENT>ValueError("<STR_LIT>")<EOL><DEDENT>if not b:<EOL><INDENT>if not ((lower + upper == <NUM_LIT:0>) or (lower == <NUM_LIT:0>)):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not (lower + upper == <NUM_LIT:0>):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>from math import floor, ceil<EOL>res = num<EOL>if not b:<EOL><INDENT>res = num<EOL>if num > upper or num == lower:<EOL><INDENT>num = lower + abs(num + upper) % (abs(lower) + abs(upper))<EOL><DEDENT>if num < lower or num == upper:<EOL><INDENT>num = upper - abs(num - lower) % (abs(lower) + abs(upper))<EOL><DEDENT>res = lower if num == upper else num<EOL><DEDENT>else:<EOL><INDENT>total_length = abs(lower) + abs(upper)<EOL>if num < -total_length:<EOL><INDENT>num += ceil(num / (-<NUM_LIT:2> * total_length)) * <NUM_LIT:2> * total_length<EOL><DEDENT>if num > total_length:<EOL><INDENT>num -= floor(num / (<NUM_LIT:2> * total_length)) * <NUM_LIT:2> * total_length<EOL><DEDENT>if num > upper:<EOL><INDENT>num = total_length - num<EOL><DEDENT>if num < lower:<EOL><INDENT>num = -total_length - num<EOL><DEDENT>res = num<EOL><DEDENT>res *= <NUM_LIT:1.0>  <EOL>return res<EOL>
Normalize number to range [lower, upper) or [lower, upper].

    Parameters
    ----------
    num : float
        The number to be normalized.
    lower : int
        Lower limit of range. Default is 0.
    upper : int
        Upper limit of range. Default is 360.
    b : bool
        Type of normalization. Default is False. See notes.

        When b=True, the range must be symmetric about 0.
        When b=False, the range must be symmetric about 0 or ``lower`` must
        be equal to 0.

    Returns
    -------
    n : float
        A number in the range [lower, upper) or [lower, upper].

    Raises
    ------
    ValueError
      If lower >= upper.

    Notes
    -----
    If the keyword `b == False`, then the normalization is done in the
    following way. Consider the numbers to be arranged in a circle,
    with the lower and upper ends sitting on top of each other. Moving
    past one limit, takes the number into the beginning of the other
    end. For example, if range is [0 - 360), then 361 becomes 1 and 360
    becomes 0. Negative numbers move from higher to lower numbers. So,
    -1 normalized to [0 - 360) becomes 359.

    When b=False range must be symmetric about 0 or lower=0.

    If the keyword `b == True`, then the given number is considered to
    "bounce" between the two limits. So, -91 normalized to [-90, 90],
    becomes -89, instead of 89. In this case the range is [lower,
    upper]. This code is based on the function `fmt_delta` of `TPM`.

    When b=True range must be symmetric about 0.

    Examples
    --------
    >>> normalize(-270,-180,180)
    90.0
    >>> import math
    >>> math.degrees(normalize(-2*math.pi,-math.pi,math.pi))
    0.0
    >>> normalize(-180, -180, 180)
    -180.0
    >>> normalize(180, -180, 180)
    -180.0
    >>> normalize(180, -180, 180, b=True)
    180.0
    >>> normalize(181,-180,180)
    -179.0
    >>> normalize(181, -180, 180, b=True)
    179.0
    >>> normalize(-180,0,360)
    180.0
    >>> normalize(36,0,24)
    12.0
    >>> normalize(368.5,-180,180)
    8.5
    >>> normalize(-100, -90, 90)
    80.0
    >>> normalize(-100, -90, 90, b=True)
    -80.0
    >>> normalize(100, -90, 90, b=True)
    80.0
    >>> normalize(181, -90, 90, b=True)
    -1.0
    >>> normalize(270, -90, 90, b=True)
    -90.0
    >>> normalize(271, -90, 90, b=True)
    -89.0