import math


def angle_difference(a1, a2):
    """
    Returns the minimal angle difference between two orientations.

    Parameters
    ----------
    a1 : float
        An angle in radians
    a2 : float
        Another angle in radians

    Returns
    -------
    angle : float
        The minimal angle difference in radians
    """
    pos1 = abs(math.pi - abs(abs(a1 - a2) - math.pi))
    if a1 < math.pi:
        pos2 = abs(math.pi - abs(abs((a1 + math.pi) - a2) - math.pi))
    elif a2 < math.pi:
        pos2 = abs(math.pi - abs(abs(a1 - (a2 + math.pi)) - math.pi))
    else:
        return pos1
    return pos1 if pos1 < pos2 else pos2