Results 1 to 10 of 10

Thread: NaN?????

  1. #1
    sabarooni's Avatar
    sabarooni is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Question 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!!

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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. #3
    sabarooni's Avatar
    sabarooni is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default 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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: NaN?????

    Quote Originally Posted by sabarooni View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sabarooni's Avatar
    sabarooni is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: NaN?????

    Quote Originally Posted by JosAH View Post
    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. #6
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #8
    sabarooni's Avatar
    sabarooni is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default 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 ;
    
    //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!!
    Last edited by pbrockway2; 11-08-2011 at 11:53 PM. Reason: code tags added

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #10
    sabarooni's Avatar
    sabarooni is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: NaN?????

    Quote Originally Posted by pbrockway2 View Post
    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
  •