function AngleDifference takes real angle1, real angle2 returns real
local real x
set angle1=ModuloReal(angle1,360)
set angle2=ModuloReal(angle2,360)
if angle1>angle2 then
set x=angle1
set angle1=angle2
set angle2=x
endif
set x=angle2-360
if angle2-angle1 > angle1-x then
set angle2=x
endif
return RAbsBJ(angle1-angle2)
endfunction