Finding the angle between two bearings is often confusing.[1]



Task

Find the angle which is the result of the subtraction b2 - b1, where b1 and b2 are the bearings.


Input bearings are expressed in the range   -180   to   +180   degrees.

The  result  is also expressed in the range   -180   to   +180   degrees.



Compute the angle for the following pairs:

 20 degrees (b1) and 45 degrees (b2)
 -45 and 45
 -85 and 90
 -95 and 90
 -45 and 125
 -45 and 145
 29.4803 and -88.6381
 -78.3251 and -159.036


Optional extra
 
Allow the input bearings to be any (finite) value.



Test cases

 -70099.74233810938 and 29840.67437876723
 -165313.6666297357 and 33693.9894517456
 1174.8380510598456 and -154146.66490124757
 60175.77306795546 and 42213.07192354373


*        Angle difference between two bearings - 06/06/2018
ANGLEDBB CSECT
         USING  ANGLEDBB,R13       base register
         B      72(R15)            skip savearea
         DC     17F'0'             savearea
         SAVE   (14,12)            save previous context
         ST     R13,4(R15)         link backward
         ST     R15,8(R13)         link forward
         LR     R13,R15            set addressability
         LA     R10,T-4            @t
         LA     R6,1               i=1
       DO WHILE=(C,R6,LE,N)        do i=1 to n
         LA     R10,4(R10)           next @t
         L      R7,0(R10)            a=t(i,1)
         LA     R10,4(R10)           next @t
         L      R8,0(R10)            b=t(i,2)
         LR     R4,R8                b
         SR     R4,R7                b-a
         SRDA   R4,32                ~
         D      R4,=F'3600000'       /360
         A      R4,=F'5400000'       +540
         SRDA   R4,32                ~
         D      R4,=F'3600000'       /360
         S      R4,=F'1800000'       x=((((b-a)//360)+540)//360)-180         
         XDECO  R7,XDEC              edit a
         MVC    PG(8),XDEC           output a
         MVC    PG+9(4),XDEC+8       output a decimals
         XDECO  R8,XDEC              edit b
         MVC    PG+14(8),XDEC        output b
         MVC    PG+23(4),XDEC+8      output b decimals
         XDECO  R4,XDEC              edit x
         MVC    PG+28(8),XDEC        output x
         MVC    PG+37(4),XDEC+8      output x decimals
         XPRNT  PG,L'PG              print
         LA     R6,1(R6)             i++
       ENDDO    ,                  enddo i
         L      R13,4(0,R13)       restore previous savearea pointer
         RETURN (14,12),RC=0       restore registers from calling sav
N        DC     F'8'               number of pairs
T        DC     F'200000',F'450000',F'-450000',F'450000'
         DC     F'-850000',F'900000',F'-950000',F'900000'
         DC     F'-450000',F'1250000',F'450000',F'1450000'
         DC     F'294803',F'-886361',F'-783251',F'-1590360'
PG       DC     CL80'12345678.1234 12345678.1234 12345678.1234'
XDEC     DS     CL12               temp
         YREGS
         END    ANGLEDBB