title: Dealing with Angle Wrap in c++ code
body: <p>Is there a way to safety and simply deal with angle wrap with the minimum number of case statements. </p>
  
  <p>Angle wrap occurs when using a particular representation for angle (either 0-360 deg or -180 - 180 deg (or equivalent in radians)) and you wrap over the angle. For example say you have an angle of -170, and you subtract 50 deg. You mathematically add up to -220 but should actually be +140 deg.</p>
  
  <p>Obviously you can check for this using:</p>
  
  <pre><code>if (deg &lt; -180) { 180 - abs(deg + 180); }
  </code></pre>
  
  <p>or similar. But firstly you need multitudes of checks and secondly it doesn't work if you wrap twice.</p>
  
  <p>The second case where this is prevalent is in the interpolation between two angles. </p>
  
  <p>For Example, say I have an angle of -170 deg and 160 deg and I want halfway in between them. A common way to do this is <code>ang1 + 0.5(ang2-ang1)</code> but in the example i have provided it will cause the angle to be -5 deg when it should be 175.</p>
  
  <p><strong>Is there a common way to handle angle wrap in these scenarios?</strong></p>