Angle in costfunction (2 pi problem)

Hi! How do I handle a cost function including angles? I.e. (THETA_ref - THETA)^2. If THETA is 0 and THETA_ref is 4pi, the error is 4pi, while in reality it is zero. I can minimize the difference between the sines or cosines (introducing non linearity in the cost function), but I wonder whether there is a nicer way.