There is a standard formula for calculating the angle between vectors:
And there are two vectors, the angle between which can not be calculated, since the right side of the equation is less than -1. These vectors are:
var x1 = -0.045797169475341334, y1 = -0.9989507591808752; var x2 = 0.04579716947534099, y2 = 0.9989507591808753; In summary, the expression:
(x1 * x2 + y1 * y2) / Math.sqrt(Math.pow(x1, 2) + Math.pow(y1, 2)) * Math.sqrt(Math.pow(x2, 2) + Math.pow(y2, 2)) gives the result: -1.0000000000000002
And if we take the arc cosine of this number, it will be NaN , which is understandable, since it is defined on the interval from -1 до 1 .
How can I adjust the formula so that this error does not exist?

absfunction forx1 * x2 + y1 * y2. - Mark Shevchenkoabscertainly won't help here. You can simply insert a type checkif (r>1 || r< -1) r = +-1(such pseudo-code). Otherwise rounding errors can not be corrected. The easiest way to override theacosfunction is to write your own with checking for an out of range[-1, 1]. - andy.37atan2. He does not give such errors. - andy.37atan2? - Khusamov Sukhrob