Assume you have 2 angles (and degrees) and you want the difference angle from phi1 to phi2, but also keep the sign for the direction (clockwise or anticlockwise). This function should return the difference angle in a range -180.0 to +180.0 degrees.
// distance between 2 angles
FUNCTION DistAngle: phi1, phi2
LOCAL dif = FMOD(phi2-phi1, 360.0)
IF dif<-180 THEN dif = 360.0 + dif
IF dif>180.0 THEN dif = 180.0 - dif
RETURN dif
ENDFUNCTION