0

The top answer in Average of two angles with wrap around is wrong per testing + the comments.

The bottom answer 12>

 math:atan(  (math:sin(180)+math:sin(270)) / (math:cos(180)+math:cos(270))).
-1.1946710584651132

but I get -1.946.. instead of the expected 225.

Erlang's math:atan isn't behaving according to http://rapidtables.com/calc/math/Arctan_Calculator.htm , however, which gives different results .

How do I find the average of 2 angles in a circle?

Edit: Attempting to use Radians. Degrees are 180 and 270.

16> A = 180 * 3.14 / 180.
3.14
17> B = 270 * 3.14 / 180.
4.71
18> S = math:sin(A) + math:sin(B).
-0.9984044934712312
19> S2 = S / 2.
-0.4992022467356156
20> C = math:cos(A) + math:cos(B).
-1.002387709839821
21> C2 = C / 2.
-0.5011938549199105
22> math:atan(S, C).
** exception error: undefined function math:atan/2
23> math:atan(S/C).
0.7834073464102068
24> math:atan(S/C) * 180 / 3.14.
44.90870138657236
25> math:atan(S2/C2) * 180 / 3.14.
44.90870138657236

Conversion: -1.19 to degrees = -68.18198.3 360 - 68 = 292. This isn't the expected 225.

quantumpotato
  • 9,637
  • 14
  • 70
  • 146

1 Answers1

2

<cos(t), sin(t)> is the unit vector with angle t in radians. So your formula adds the two unit vectors and and then finds the angle of the resultant vector.

Just use radians instead of degrees and use atan2 instead of atan (which puts the angle on the correct quadrant) and you should see your formula works correctly.

math:atan2( math:sin(t1)+math:sin(t2), math:cos(t1)+math:cos(t2))

Where this formula does not work is if the two angles are precisely 180 degrees apart from each other. In this case the resultant vector is the zero vector and atan is undefined.

MFisherKDX
  • 2,840
  • 3
  • 14
  • 25
  • math:atan2( math:sin(180)+math:sin(270), 29> math:cos(180)+math:cos(270)). -1.1946710584651132. I'm trying to get an answer in degrees. – quantumpotato Nov 04 '17 at 03:14
  • Yeah. You need to use radians and you need to use atan2. You can convert back to degrees when done. – MFisherKDX Nov 04 '17 at 03:18
  • @quantumpotato if this works for you please consider accepting the answer. – MFisherKDX Nov 04 '17 at 03:35
  • Please see my edit of the original question "conversion". I tried what you suggested in comments. Please use my sample code and edit your answer if you have the working answer – quantumpotato Nov 04 '17 at 04:39
  • 1
    @quantumpotato 180 deg = pi radians. 270 deg = 3pi/2 radians. sin(pi)+sin(3pi/2) = -1. cos(pi)+cos(3pi/2)=-1. atan2(-1,-1)=5pi/4 radians or 225 deg. – MFisherKDX Nov 04 '17 at 04:48