Processing - cosine rule (sss) - NaN

I am working on a simple inverse kinematic controller of a delta robot with processing. I adhere to the rule of cosines. I have a length of three sides and want to get the corners. But float angle = acos((sq(humerus)+sq(ulna) - sq(radius))/(2 * humerus * ulna)); always returns NaN. Any ideas?

+4
source share
2 answers

You probably want to use the law of cosines if you know the parties:

enter image description here

In Java terms, to solve the angle C (opposite the side of length C ) we would have

 Math.acos((a*a + b*b - c*c) / (2*a*b)) 

There are several reasons why you can get NaN :

  • One of your sides is negative, so when you are the square of the root, you get NaN .
  • Your triangle cannot exist depending on the side lengths you specify. Check out this documentation for acos :

    If the NaN argument or its absolute value is greater than 1 , then the result is NaN .

+2
source

If you check the javadoc for Math.acos , you will see the following:

 If the argument is NaN or its absolute value is greater than 1, then the result is NaN. 

So, there are two possibilities:

  • The numerator is larger than the denominator in absolute value, resulting in a fraction that exceeds 1.0. Therefore, arcoscosine will return NaN.

  • One of the square roots returns NaN. As seen from javadoc : If the argument is NaN or less than zero, then the result is NaN.

So, I would check your values ​​for the humerus, radius and ulna. Perhaps you either made a numerator too large (for example, using a floating point error), or you had a vector and not a scalar value for measuring bone tissue (which led to a negative rather than positive argument of the square root).

+1
source

Source: https://habr.com/ru/post/1447280/


All Articles