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


INCLUDE "H6:REALMATH.ACT"
 
INT FUNC AngleI(INT a1,a2)
  INT r
 
  r=a2-a1
  WHILE r>180 DO r==-360 OD
  WHILE r<-180 DO r==+360 OD
RETURN (r)
 
PROC TestI(INT a1,a2)
  INT r
 
  r=AngleI(a1,a2)
  PrintF("%I .. %I = %I%E",a1,a2,r)
RETURN
 
PROC AngleR(REAL POINTER r1,r2,r)
  REAL tmp,r180,rm180,r360
 
  ValR("180",r180)
  ValR("-180",rm180)
  ValR("360",r360)
 
  RealSub(r2,r1,r)
  WHILE RealGreaterOrEqual(r,r180)
  DO
    RealSub(r,r360,tmp) RealAssign(tmp,r)
  OD
  WHILE RealGreaterOrEqual(rm180,r)
  DO
    RealAdd(r,r360,tmp) RealAssign(tmp,r)
  OD
RETURN
 
PROC TestR(CHAR ARRAY s1,s2)
  REAL r1,r2,r
 
  ValR(s1,r1) ValR(s2,r2)
  AngleR(r1,r2,r)
  PrintR(r1) Print(" .. ") PrintR(r2)
  Print(" = ") PrintRE(r)
RETURN
 
PROC Main()
  Put(125) PutE() ;clear screen
  TestI(20,45)
  TestI(-45,45)
  TestI(-85,90)
  TestI(-95,90)
  TestI(-45,125)
  TestI(-45,145)
  TestR("29.4803","-88.6381")
  TestR("-78.3251","-159.036")
  TestR("-70099.74233810938","29840.67437876723")
  TestR("-165313.6666297357","33693.9894517456")
  TestR("1174.8380510598456","-154146.66490124757")
  TestR("60175.77306795546","42213.07192354373")
RETURN