import math


def normalize_theta(theta, degrees=True):
    """Normalize angle theta to -180 and 180 degrees
    Input:
        theta (float): angle in degrees or radians
        degrees (bool, default True): if True theta in degrees else in radians
    """
    if degrees:
        pi = 180.0
    else:
        pi = math.pi

    if theta >= 2 * pi or theta <= -2 * pi:
        theta = theta % (2 * pi)

    if theta <= pi and theta > -pi:
        return theta

    elif theta > pi:
        theta -= 2 * pi
        return theta

    else:
        theta += 2 * pi
        return theta