title: Calculate angle between 3 points with vector, more precision needed (java)
body: <p>I need to calculate the angle between 3 points. I've done that using vectors, it looks like it's working, but sometimes I get NaN as a result. To calculate the angle I used the arcos(dot(v1,v2)/(length(v1)*length(v2))) formula. Here's the code:</p>
  
  <pre><code>private static double angleBetween(Point previous, Point center, Point next) {
          Vector2D vCenter = new Vector2D(center.x, center.y );
          Vector2D vPrev = new Vector2D(previous.x, previous.y );
          Vector2D vNext = new Vector2D(next.x, next.y );
  
          Vector2D a = vCenter.minus(vPrev);
          Vector2D b = vCenter.minus(vNext);
  
          double dot = Vector2D.dot(a, b);
  
          double la = a.length();
  
          double lb = b.length();
  
          double l = la*lb;
  
          double acos = Math.acos(dot/l);
  
          double deg = Math.toDegrees(acos);
  
          return deg;
  
          }
  </code></pre>
  
  <p>Vector2D class:</p>
  
  <pre><code>public double length() {
          return Math.sqrt(x * x + y * y);
      };
  
  public static double dot(Vector2D v1, Vector2D v2) {
          return v1.x * v2.x + v1.y * v2.y;
      };
  
  public Vector2D minus(Vector2D v) {
          return new Vector2D(x - v.x, y - v.y);
      };
  </code></pre>
  
  <p>Debugging the program I've discovered why this happens. for example let be:</p>
  
  <pre><code>center = (127,356)
  previous = (117,358)
  next = (137,354)
  
  //calculating the vectors
  a = (-10,2) //center - prev
  b = (10,-2) //center - next
  
  dot = -10*10 + 2*-2 = 104
  
  la = sqrt(-10*-10 + 2*2) = sqrt(104) = see attachment
  lb = sqrt(10*10 + -2*-2) = sqrt(104) = see attachment
  
  l = la * lb = see attachment
  </code></pre>
  
  <p><code>acos = NaN</code> because <code>dot/l&gt;1</code> because I lost precision because of <code>sqrt()</code> which didn't give me the exact value therefore <code>la*lb</code> isn't 104.</p>
  
  <p>now as far as I know double is the most precise number type in java. How can I solve this problem?</p>
  
  <p><a href="https://i.stack.imgur.com/O9fVL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O9fVL.png" alt="attchment: la,lb,l values"></a></p>
  
  <p>PS It may looks like a very rare situation, but I'm experiencing quite a lot of them, so I can't just ignore it.</p>