NaN?????

• 11-08-2011, 04:53 PM
sabarooni
NaN?????
Hi, I can't understand why this is coming up as NaN.

trying to d oa program which can work out 2 angles and a side of a triangle when the other 2 sides and angle are inputed.

here is the bit which comes up as NaN (last line)

System.out.print ("Side c = ") ;
System.out.println (+tm.cossidec (sidea, sideb, tm.d2r(angleA, tm.constant))) ;
System.out.print ("Convert Angle A to Radians: ") ;
System.out.println (tm.d2r(angleA, tm.constant)) ;
System.out.print ("Angle B = ") ;
System.out.println (tm.cosangleB (sidea, sideb, tm.cossidec (sidea, sideb, tm.d2r(angleA, tm.constant)))) ;

And here is the maths class (constant is 180)

public double cossidec (double num1, double num2, double num3)
{
return Math.sqrt((Math.pow(num1, 2))+(Math.pow(num2, 2))-((2*num1*num2)*Math.cos(num3))) ;
}

public double cosangleB (double num1, double num2, double num3)
{
return Math.acos((Math.pow(num1, 2))+(Math.pow(num2, 2))-(Math.pow(num3, 2))/(2*num1*num3)) ;
}

public double cosangleC (double num1, double num2, double num3)
{
return Math.acos((Math.pow(num1, 2))+(Math.pow(num3, 2))-(Math.pow(num2, 2))/(2*num1*num2)) ;
}
}

anyone have any idea why it comes up as NaN?

• 11-08-2011, 05:01 PM
tnrh1
Re: NaN?????
"NaN" stands for "not a number". "Nan" is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.

0.0 / 0.0 -> NaN
Math.sqrt(-2.0) -> NaN
• 11-08-2011, 06:53 PM
sabarooni
Re: NaN?????
hey I know what it means and stuff, but I cna't understand why it comes up on my program, when the inputs (sidea, sideb, angleA)are all positive.
• 11-08-2011, 09:46 PM
JosAH
Re: NaN?????
Quote:

Originally Posted by sabarooni
anyone have any idea why it comes up as NaN?

What is 'it'? There are several values being calculated in your code.

kind regards,

Jos
• 11-08-2011, 11:06 PM
sabarooni
Re: NaN?????
Quote:

Originally Posted by JosAH
What is 'it'? There are several values being calculated in your code.

kind regards,

Jos

It is working out the side and angles of a triangle using tyhe cosine formula. This is the user input section of the code: (I just dont understand why it can't calculate it

double sidea, sideb, sidec ;
double angleA, angleB, angleC ;

//////////////////////////////////////////////////////////////////
//INPUT
//////////////////////////////////////////////////////////////////

System.out.print ("Enter side a of the triangle: ") ;
sidea=input.nextDouble () ;
System.out.print ("Enter side b of the triangle: ") ;
sideb=input.nextDouble () ;
System.out.print ("Enter angle A of the triangle: ") ;
angleA=input.nextDouble () ;

//////////////////////////////////////////////////////////////////
//INSTANTIATE A COPY OF TRIANGLEMATHS
//////////////////////////////////////////////////////////////////

Trianglemaths tm = new Trianglemaths () ;

//////////////////////////////////////////////////////////////////
//OUTPUT
//////////////////////////////////////////////////////////////////
• 11-09-2011, 12:12 AM
tnrh1
Re: NaN?????
Dude please use the code tag.
debug it, you will be able to see the variable values and see the reason to this mess.
• 11-09-2011, 12:16 AM
pbrockway2
Re: NaN?????
Check your data. Ie use System.out.println() to ensure that the values are what you think they are.

Check your formulae. Especially the angle ones.

Then, if you still have a problem, address Jos' question (what is 'it'?) by posting compilable, runnable code and giving its output for given (stated) input.
• 11-09-2011, 12:32 AM
sabarooni
Re: NaN?????
Hi I'm not sure what you mean by code tag, as I'm new to this site, but here is the code. there are 2 classes, Triangle and Trianglemaths. So far it only prints out the side which I am looking for (side c) and it can convert the degree input into radians, but it doesn't like working out the first angle (Angle B) for some reason

Triangle
Code:

```// // // // // import java.util.Scanner ; class Triangle {         static Scanner input = new Scanner (System.in) ;                 public static void main (String args [])         {         ////////////////////////////////////////////////////////////////// //DECLERATIONS //////////////////////////////////////////////////////////////////                                 double sidea, sideb, sidec ;                 double angleA, angleB, angleC ;                 ////////////////////////////////////////////////////////////////// //INPUT //////////////////////////////////////////////////////////////////                 System.out.print ("Enter side a of the triangle: ") ;                 sidea=input.nextDouble () ;                 System.out.print ("Enter side b of the triangle: ") ;                 sideb=input.nextDouble () ;                 System.out.print ("Enter angle A of the triangle: ") ;                 angleA=input.nextDouble () ;                 ////////////////////////////////////////////////////////////////// //INSTANTIATE A COPY OF TRIANGLEMATHS //////////////////////////////////////////////////////////////////                 Trianglemaths tm = new Trianglemaths () ;                 ////////////////////////////////////////////////////////////////// //OUTPUT //////////////////////////////////////////////////////////////////                 //System.out.print (+ tm.d2r (angleA, tm.constant)) ;                 System.out.print ("Side c = ") ;                 System.out.println (+tm.cossidec (sidea, sideb, tm.d2r(angleA, tm.constant))) ;                 System.out.print ("Convert Angle A to Radians: ") ;                 System.out.println (tm.d2r(angleA, tm.constant)) ;                 System.out.print ("Angle B = ") ;                 System.out.println (tm.cosangleB (sidea, sideb, tm.cossidec (sidea, sideb, tm.d2r(angleA, tm.constant)))) ;                         } }        Trianglemaths // // // // class Trianglemaths { ////////////////////////////////////////////////////////////////// //CONSTANTS //////////////////////////////////////////////////////////////////                 public final double constant = 180 ; //DEGREES TO RADIAN CONVERSION //////////////////////////////////////////////////////////////////         public double d2r (double num1, final double constant)         {                 return num1*(Math.PI/constant) ; //this converts degrees to radians using the formula 'x*(pi/180)'                         }         ////////////////////////////////////////////////////////////////// //COSINE FORMULAE //////////////////////////////////////////////////////////////////         public double cossidec (double num1, double num2, double num3)         {                 return Math.sqrt((Math.pow(num1, 2))+(Math.pow(num2, 2))-((2*num1*num2)*Math.cos(num3))) ;         }                 public double cosangleB (double num1, double num2, double num3)         {                 return Math.acos((Math.pow(num1, 2))+(Math.pow(num2, 2))-(Math.pow(num3, 2))/(2*num1*num3)) ;         }                 public double cosangleC (double num1, double num2, double num3)         {                 return Math.acos((Math.pow(num1, 2))+(Math.pow(num3, 2))-(Math.pow(num2, 2))/(2*num1*num2)) ;         } }```
thanks!!
• 11-09-2011, 12:55 AM
pbrockway2
Re: NaN?????
Quote:

I'm not sure what you mean by code tag
When you post code you put [code] at the start of the code and [/code] at the end. That helps keep the indentation when the code appears on a web page.
• 11-09-2011, 01:36 AM
sabarooni
Re: NaN?????
Quote:

Originally Posted by pbrockway2
When you post code you put [code] at the start of the code and [/code] at the end. That helps keep the indentation when the code appears on a web page.

ah awesome thanks!