Results 1 to 10 of 10
Thread: NaN?????
- 11-08-2011, 03:53 PM #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?
Thanks in advance!!
- 11-08-2011, 04:01 PM #2
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
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, 05:53 PM #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.
- 11-08-2011, 08:46 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 11-08-2011, 10:06 PM #5
Re: NaN?????
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-08-2011, 11:12 PM #6
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
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-08-2011, 11:16 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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-08-2011, 11:32 PM #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
thanks!!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 ; //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)) ; } }Last edited by pbrockway2; 11-08-2011 at 11:53 PM. Reason: code tags added
- 11-08-2011, 11:55 PM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: NaN?????
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.I'm not sure what you mean by code tag
- 11-09-2011, 12:36 AM #10


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks