def canonicalize_half_turns(
        half_turns: Union[sympy.Basic, float]
) -> Union[sympy.Basic, float]:
    """Wraps the input into the range (-1, +1]."""
    if isinstance(half_turns, sympy.Basic):
        return half_turns
    half_turns %= 2
    if half_turns > 1:
        half_turns -= 2
    return half_turns