Angle difference between two bearings

func bearingAngleDiff(b1, b2) {
    (var b = ((b2 - b1 + 720) % 360)) > 180 ? (b - 360) : b
}

printf("%25s %25s %25s\n", "B1", "B2", "Difference")
printf("%25s %25s %25s\n", "-"*20, "-"*20, "-"*20)


for b1,b2 in ([
                       20,                       45
                      -45,                       45
                      -85,                       90
                      -95,                       90
                      -45,                      125
                      -45,                      145
                  29.4803,                 -88.6381
                 -78.3251,                 -159.036
       -70099.74233810938,        29840.67437876723
       -165313.6666297357,         33693.9894517456
       1174.8380510598456,      -154146.66490124757
        60175.77306795546,        42213.07192354373
    ].slices(2)
) {
    printf("%25s %25s %25s\n", b1, b2, bearingAngleDiff(b1, b2))
}

Output:

                       B1                        B2                Difference
     --------------------      --------------------      --------------------
                       20                        45                        25
                      -45                        45                        90
                      -85                        90                       175
                      -95                        90                      -175
                      -45                       125                       170
                      -45                       145                      -170
                  29.4803                  -88.6381                 -118.1184
                 -78.3251                  -159.036                  -80.7109
       -70099.74233810938         29840.67437876723          -139.58328312339
       -165313.6666297357          33693.9894517456            -72.3439185187
       1174.8380510598456       -154146.66490124757        -161.5029523074156
        60175.77306795546         42213.07192354373            37.29885558827