1. ## 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?

2. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
6

## 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

3. ## 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.

4. ## Re: NaN?????

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

5. ## Re: NaN?????

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
//////////////////////////////////////////////////////////////////

6. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
6

## 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.

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## 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.

8. ## 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
Java 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 ;

//////////////////////////////////////////////////////////////////

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!!
Last edited by pbrockway2; 11-09-2011 at 12:53 AM. Reason: code tags added

9. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: NaN?????

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.

10. ## Re: NaN?????

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!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•