import torch
import math


def closest_angle_error(angle_a: torch.Tensor, angle_b: torch.Tensor) -> torch.Tensor:
    """ Finds the closest angle between angle_b - angle_a in radians.

    :param angle_a: a Tensor of angles in radians
    :param angle_b: a Tensor of angles in radians
    :return: The relative angle error between A and B between [0, pi]
    """
    assert angle_a.shape == angle_b.shape
    two_pi = 2.0 * math.pi
    wrapped = torch.fmod(angle_b - angle_a, two_pi)
    closest_angle = torch.fmod(2.0 * wrapped, two_pi) - wrapped
    return torch.abs(closest_angle)