import math


def std_periodic_var(p, mid=0.0, pi=math.pi):
    """
    Transform a periodic variable into its range.

    >>> std_periodic_var(math.pi)
    -3.1415...

    >>> std_periodic_var(2*math.pi + 0.01)
    0.0...

    :param p: Value
    :param mid: The middle value
    :param pi: Half-range
    :return: The transformed value
    """
    twopi = 2 * pi
    while p < mid - pi:
        p += twopi
    while p >= mid + pi:
        p -= twopi
    return p