Calculate difference of two numbers on the 360 circle with the sign/direction

116 Views Asked by At

There are a couple of answers to this question, but they leave out the sign. Is there a clever way to determine if the angle is positive or negative?

This nice formula gives the angle, but not the direction - any clever hints? $$ d(,) = \min\{360−|A-B|,|A-B|\} $$

3

There are 3 best solutions below

1
On

If I understand correctly, you are given a pair of angles $A$ and $B$ on the standard $360^\circ$ circle and would like to know the direction of the difference angle between them.

Firstly, the actual difference formula you cite assumes $A,B$ are between $0^\circ$ and $360^\circ$, it won't work with larger angles. For example, $720^\circ = 0^\circ$ since it's just 2 rotations around the full circle, and so is $1440^\circ$, but your formula will not give $0^\circ$, just be careful with it.

The direction of the difference is given by the sign of the actual difference $A-B$. In other words, the difference is positive when $$ A > B \iff A - B > 0 \iff \mathrm{sign}(A-B) = 1. $$

1
On

sign map of d(A,B) You have to make a choice which sign to chose. Turning A towards B or turning B towards A. This is my choice. The sing flips when you cross the 180 degree difference angle I would say. Try to turn this into a function.

0
On

I found a formula that works - seems trivial now that I have it.

=MIN(360-ABS(A-B),ABS(A-B))*(IF(ABS(A-B)>180,-1,1)*SIGN(A-B))

This is the excel formula I use and it works fine. A and B are the inputs and of course in Excel it would be A1 or A2 or ... and the same for B1 B2, etc.

The MIN function gets me the number of degrees difference between the two angles on the circle. To get the sign, I subtract A and B, and if the difference between the two angles is greater than 180, I invert the sign. Thanks to those of you that helped me find this solution!