Thread: -1.#J output

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    18

    -1.#J output

    Anyone have a clue as to why this would be happening?

    here's the code:

    Code:
    void solve_SSS (double elmnt_1, double elmnt_2, double elmnt_3){  /*begin SSS solver*/
    
    	double ttl = 180;  /*initialize ttl as 180*/
    	double A_num;  /*set angle A numerator as double*/
    	double A_denom;  /*set angle A denominator as double*/
    	double B_num;  /*set angle B numerator as double*/
    	double B_denom;  /*set angle B denominator as double*/
    	triangle s_SSS;  /*triangle typedef*/
    
    	s_SSS.side_a = elmnt_1;  /*side a = elmnt_1*/
    	s_SSS.side_b = elmnt_2,  /*side b = elmnt_2*/
    	s_SSS.side_c = elmnt_3;  /*side c = elmnt_3*/
    	A_num = (pow(s_SSS.side_b,2)+pow(s_SSS.side_c,2)-pow(s_SSS.side_a,2));  /*calc A numerator*/
    	A_denom = (2*s_SSS.side_b*s_SSS.side_c);  /*calc A denom*/
    	s_SSS.ang_A = acos(A_num/A_denom)*deg;  /*calc angle A*/
    	B_num = (pow(s_SSS.side_c,2)+pow(s_SSS.side_a,2)-pow(s_SSS.side_b,2));  /*calc B numerator*/
    	B_denom = (2*s_SSS.side_a*s_SSS.side_c);  /*calc B denom*/
    	s_SSS.ang_B = acos(B_num/B_denom)*deg;  /*calc angle B*/
    	s_SSS.ang_C = ttl - s_SSS.ang_A - s_SSS.ang_B;  /*calc angle C*/
    	
    	/*printf ("%.2f\n", A_num);
    	printf ("%.2f\n", A_denom);*/
    
    
    	printf ("Your solved triangle:"); /*print solved triangle*/
    	printf ("\n");
    	printf ("angle A = %.2f\n", s_SSS.ang_A);
    	printf ("angle B = %.2f\n", s_SSS.ang_B);
    	printf ("angle C = %.2f\n", s_SSS.ang_C);
    	printf ("side a = %.2f\n", s_SSS.side_a);
    	printf ("side b = %.2f\n", s_SSS.side_b);
    	printf ("side c = %.2f\n", s_SSS.side_c);
    
    	return;  /*end function*/

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Why what is happening?

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    18
    I get an output of -1.#J for angle A, angle B, and Angle C

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Is variable "deg" initialized anywhere?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    -1.#J is a sign that you are getting +/-inf or NaN as a result. The J is a result of rounding to get the .2 part of %.2f. Plain old %f might tell you which. If you're just leaving doubles uninitialized, its probably NaN though. Be aware that other math errors (such as dividing by zero) can cause this in floating-point types.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    In cases of "degenerate triangles" some sides will have 0 length, and you will get division by 0. Put in some error checking on all your denominator terms to make sure they are nonzero before you divide.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    18
    ahhh yes...it was a divide by zero problem. Thanks for the help everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM