title: Bearing calculation in C#
body: <p>I want to calculate bearing between 2 GPS positions, I foollowed <a href="https://www.movable-type.co.uk/scripts/latlong.html" rel="nofollow noreferrer">this page</a> recommandations for my algorythm:</p>
  
  <pre><code>    public static double Bearing(IPointGps pt1, IPointGps pt2)
      {
          double x = Math.Cos(pt1.Latitude) * Math.Sin(pt2.Latitude) - Math.Sin(pt1.Latitude) * Math.Cos(pt2.Latitude) * Math.Cos(pt2.Longitude - pt1.Longitude);
          double y = Math.Sin(pt2.Longitude - pt1.Longitude) * Math.Cos(pt2.Latitude);
  
          // Math.Atan2 can return negative value, 0 &lt;= output value &lt; 2*PI expected 
          return (Math.Atan2(y, x) + Math.PI * 2)%(Math.PI * 2);
      }
  </code></pre>
  
  <p>Then I transform my value in degrees using this method</p>
  
  <pre><code>    public static double RadiansToDegrees(double angle)
      {
          return (angle * 180.0) / Math.PI;
      }
  </code></pre>
  
  <p>I have the following test sample:</p>
  
  <ul>
  <li>Point1 (lat, long) = 43.6373638888888888888888888888889, 1.35762222222222222222222222222222</li>
  <li>Point2 (lat, long) = 43.6156444444444444444444444444444,1.380225</li>
  <li>Expected bearing = 323°</li>
  </ul>
  
  <p>However, I obtain a bearing of 315.5° (5.5062235835910762 rad). If i calculate the expected radian value, i get 5.637413 which leaves no doubt that my problem lies in my bearing method.</p>
  
  <p>I already implemented other computation methods using .Net Math package (including Cos, Sin, Tan and ATan methods) and my unit tests pass with 1e-12 precision. What am I missing?</p>
  
  <p>PS: I also tryied to reimplement the Atan2 method in case there is a lack of precision in it. I obtain the very same result</p>
  
  <p>edit: My Latitude and Longitude are double as per the following interface</p>
  
  <pre><code>public interface IPointGps
  {
      double Latitude { get; }
      double Longitude { get; }
  }
  </code></pre>