Problem with Math class

Im writing a program that controlls a simulated plane. I need to use the following equaion to find the nececary angles

Some times this formula returns NaN but it is supposed to give a biggish angle

Whats wrong with it?

angleR = Math.acos(((1)+(distN*distN)-(distF*distF))/(2*(distN)));

dist N,F are always positive.

please help!!

[367 byte] By [benjisa] at [2007-10-3 4:22:50]
# 1
well, acos returns NaN when the argument is outside of the range -1 to 1. What is the value of ((1)+(distN*distN)-(distF*distF))/(2*(distN)) that gives you NaN and why do you get it?
jsalonena at 2007-7-14 22:25:10 > top of Java-index,Java Essentials,Java Programming...
# 2

> angleR = Math.acos(((1)+(distN*distN)-(distF*distF))/(2*(distN)));

>

> dist N,F are always positive.

I'm shortening your formula a bit:

(1+n*n-f*f)/(2*n)

acos(x) is undefined for x > 1, so if:

(1+n*n-f*f)/(2*n) > 1 >

(1+n*n-f*f) > 2*n >

n*n-2*n-f*f+1 > 0 >

If the last inequality holds, you get a NaN from the Math.acos() method.

The same applies for acos(x) where x < -1.

kind regards,

Jos

JosAHa at 2007-7-14 22:25:10 > top of Java-index,Java Essentials,Java Programming...
# 3

I am navigating a simulated plane to different waypoints. I use a lot of angles to calculate where the plane must go.

I have an x;y point simulating a waypoint and a front and back position of a simulated plane. distN is the distance from the waypoint to a simulated position of where the plane will be next. distF is from the waypoint to the front of the plane and distB is from the waypoint to the back. angleR is supposed to be between 0 and 180. The program is either returing NaN or something like 2 to 1. What is wrong here?

From angleR I calculate other angles that are used to calculate where the plane must move to and how much the rudder must move. If angleR is calculated wrong the the whole program wont work.

angleR = Math.acos(((1)+(distN*distN)-(distF*distF))/(2*(distN)));

please help me as this is a science project and I'm running out of time.

benjisa at 2007-7-14 22:25:10 > top of Java-index,Java Essentials,Java Programming...
# 4

You've had as much answer as anyone can give you from the info you've supplied. The argument for your acos is either out of range, or NaN itself.

You need to log the values and see at what point they are going wrong. Then you need to track down why the numbers, or the formula, are wrong.

This isn't a problem with the maths Class, it's a problem with your maths.

Usually formula using atan2 are more stable, by the way.

malcolmmca at 2007-7-14 22:25:10 > top of Java-index,Java Essentials,Java Programming...