Thread: Trying to calc solution to cubic

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    Trying to calc solution to cubic

    Ok this is what ive done so far:


    Code:
    /*
    **  Solve a cubic polynomial 
    **  By xxxxxxxxxxxxxxxx
    */
    
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    float a1,a2,a3,b,c,d,G,roots,S,x1,x2,x3, C, theta, P, Q;
    
    int main(int argc, char* argv[])
    {
    
    	
    	printf("Please enter the coefficient of x^2:");
    		scanf("%f",&a1);
    		printf("Please enter the coefficient of x:");
    		scanf("%f",&a2);
    		printf("Please enter the constant term:");
    		scanf("%f",&a3);
    		
        Q=((a1*a1-3*a2)/9);
    	S = ((2*a1*a1*a1)-(9*a1*a2+(27*a3)))/54;
    	theta=acos(S/sqrt(Q*Q*Q));
    	P= pow ( (sqrt( (S*S)-(Q*Q*Q)+fabs(S) )), 1/3);
    
    	C = ((Q*Q*Q)-(S*S));
    
    	if (C<=0)
    	{
    		theta=acos(S/sqrt(Q*Q*Q));
    		x1 = (((-2*sqrt(Q))*(cos(theta/3))) - a1)/3;
    		x2 = ((-2*sqrt(Q))*(cos((theta+2*3.1412)))/3) - a1/3;
    		x3 = ((-2*sqrt(Q))*(cos((theta+4*3.1412)))/3) - a1/3;
    		printf("First root: %f.\n", x1);
    		printf("Second root: %f.\n", x2);
    		printf("Third root: %f.\n", x3);
    	}
    	else {
    		
    			if(G>0) {
    				
    				x1 = -G*(P + (Q/P))-(a1/3);
    				printf("Only root: %f.\n", x1);
    			
    			} else {
    				x1 = G*(P + (Q/P))-(a1/3);
    			
    			}
    			printf("There is only one single root: %f.\n", x1);
               }
    
    return(0);
    }
    However it doesnt work for all values? More importantly I get this also:

    Please enter the coefficient of x^2:9
    Please enter the coefficient of x:9
    Please enter the constant term:9
    There is only one single root: -1.#IND00.
    Press any key to continue

    I got that with 9,9,9. What does -1.#IND00 mean?

    Any help would be greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    #INF are infinities, which basically correspond to overflow
    #IND are indefinites, which basically correspond to underflow. They are essentially smaller than the smallest possible float, but still bigger than zero.
    There is also
    #NAN which is Not A Number.

    The first obvious problem is 1/3 will be zero (by integer arithmetic rules), but I suspect you want 1.0/3.0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    Quote Originally Posted by Salem View Post
    #INF are infinities, which basically correspond to overflow
    #IND are indefinites, which basically correspond to underflow. They are essentially smaller than the smallest possible float, but still bigger than zero.
    There is also
    #NAN which is Not A Number.

    The first obvious problem is 1/3 will be zero (by integer arithmetic rules), but I suspect you want 1.0/3.0.
    Thanks Salem, here is the updated code, but still doesn't work!

    Code:
    /*
    **  Solve a cubic polynomial 
    **  By x
    */
    
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    float a1,a2,a3,b,c,d,G,roots,S,x1,x2,x3, C, theta, P, Q;
    
    int main(int argc, char* argv[])
    {
    
    	
    	printf("Please enter the coefficient of x^2:");
    		scanf("%f",&a1);
    		printf("Please enter the coefficient of x:");
    		scanf("%f",&a2);
    		printf("Please enter the constant term:");
    		scanf("%f",&a3);
    		
        Q=((a1*a1-3*a2)/9.0);
    	S = ((2*a1*a1*a1)-(9*a1*a2+(27*a3)))/54.0;
    	theta=acos(S/sqrt(Q*Q*Q));
    	P= pow ( (sqrt( (S*S)-(Q*Q*Q)+fabs(S) )), 1/3);
    
    	C = ((Q*Q*Q)-(S*S));
    
    	if (C<=0)
    	{
    		theta=acos(S/sqrt(Q*Q*Q));
    		x1 = (((-2*sqrt(Q))*(cos(theta/3))) - a1)/3.0;
    		x2 = ((-2*sqrt(Q))*(cos((theta+2*3.1412)))/3.0) - a1/3.0;
    		x3 = ((-2*sqrt(Q))*(cos((theta+4*3.1412)))/3.0) - a1/3.0;
    		printf("First root: %f.\n", x1);
    		printf("Second root: %f.\n", x2);
    		printf("Third root: %f.\n", x3);
    	}
    	else {
    		
    			if(G>0) {
    				
    				x1 = -G*(P + (Q/P))-(a1/3.0);
    				printf("Only root: %f.\n", x1);
    			
    			} else {
    				x1 = G*(P + (Q/P))-(a1/3.0);
    			
    			}
    			printf("There is only one single root: %f.\n", x1);
               }
    
    return(0);
    }
    But I still get the same errors

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Except the one which really needed to change is the same
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    Sorry, you meant to change this line?

    P= pow ( (sqrt( (S*S)-(Q*Q*Q)+fabs(S) )), 1.0/3.0);

    I have changed it, still gets an error. Closes with no answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Solution' and 'Project' usage in VC++
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2007, 09:50 AM
  2. Cubic equation program not giving me right answers
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2006, 05:42 PM
  3. anyone know the solution?
    By heeroyung in forum C Programming
    Replies: 15
    Last Post: 09-30-2005, 06:46 AM
  4. Cubic Root by Newton's Iterative Method
    By amirahasanen1 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2005, 02:05 PM
  5. My Unix/Linux SECURITY SOLUTION - pls read
    By bjdea1 in forum Linux Programming
    Replies: 3
    Last Post: 04-11-2004, 09:28 PM