How to calculate relative degree changes in 0 to 360. I'm working on a project that measures wind direction and I'm stuck on this what appears to be a simple degree problem. 
Example: Lets say I'm a compass (0' .. 360' ) now I'm pointing due north 0' , I want to take readings every minute and now I want to calculate the the minimal difference (relative to last reading) in degrees from the last reading..That is i want to calculate the shortest rotation in degrees that would get me to the current value

Assume we start at 0'
Reading 1:  0'   difference=0'
Reading 2:  10'  diff=10' (aka rotate right)
Reading 3:  350  diff=20' (rotate left)
reading 4:  180  diff=170' (rotate left)


My issue is that something like
 CurrentDeg- LastDeg =degChange  doesn't work for boundary case for example 
0 - 350 = -340 (It didnt roate 340 degreess but only 10', looking for minimal degree change - aka Relative offset)

Any help would be appreciated
 A: break this up into cases:
case1: absolute value of difference in angles is less or equal to 180 degrees. this is the only case you are considering.
case2: absolute value in difference in angles is greater than 180 degrees. If this is the case, then take  360-(difference in angles)
So in psudo-code, it would look something like: 
Degree_Change=|current_degree-last_degree| is what you initially calculate. But you have to make sure that it is less than or equal to 180 degrees, so you look at a couple of cases:

if Degree_Change $\leq$ 180 degrees then Degree_Change=|current_degree-last_degree|

so you do not need to modify it

if Degree_Change $\ge$ 180 degrees then Degree_Change=360-|current_degree-last_degree|

subtracting by 360 gives you an angle less than 180.
A: The problem you describe can be solved in Python as follows:
In [1]: def angle_diff_abs(x, y): 
            if abs(x-y) <= 180: 
                z = abs(x-y)
            else: 
                z = 360 - abs(x-y)          
            return(z)

For a selection of tuples:
In [2]: tuples = [(30, 90), (90, 30), (20, 340), (340, 20), 
                  (1, 359), (359, 1), (0, 180), (42, 42)] 

... here is the output:
In [3]: for tuplex in tuples: 
            z = angle_diff_abs(tuplex[0], tuplex[1])
            print(f"for {tuplex} is z = {z}")

Out [3]: for (30, 90) is z = 60
         for (90, 30) is z = 60
         for (20, 340) is z = 40
         for (340, 20) is z = 40
         for (1, 359) is z = 2
         for (359, 1) is z = 2
         for (0, 180) is z = 180
         for (42, 42) is z = 0

If you also need to determine the direction of the change (i.e. clockwise or counterclockwise), this becomes a little more complex:
In [4]: def angle_diff_rel(x, y): 
            if abs(x-y) <= 180: 
                if x <= y: 
                    z = abs(x-y)
                else:  # x > y
                    z = y-x
            else: 
                if x < y: 
                    z = abs(x-y) - 360
                else:  # x > y
                    z = 360 - abs(x-y)
            return(z)

For the selection of tuples from before, here is the output:
In [5]: for tuplex in tuples: 
            z = angle_diff_rel(tuplex[0], tuplex[1])
            print(f"for {tuplex} is z = {z}")

Out [5]: for (30, 90) is z = 60
         for (90, 30) is z = -60
         for (20, 340) is z = -40
         for (340, 20) is z = 40
         for (1, 359) is z = -2
         for (359, 1) is z = 2
         for (0, 180) is z = 180
         for (42, 42) is z = 0

