import math


def fixAnglePlusMinusPi(a):
    """
    A is an angle in radians;  return an equivalent angle between plus
    and minus pi
    """
    pi2 = 2.0 * math.pi
    i = 0
    while abs(a) > math.pi:
        if a > math.pi:
            a = a - pi2
        elif a < -math.pi:
            a = a + pi2
        i += 1
        if i > 10: break  # loop found
    return a