Thread: Cubic equation program not giving me right answers

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Cubic equation program not giving me right answers

    Im getting the wrong roots when running the program. The maths the program is based on is in the attatched image.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define pi 3.14159265
    
    int sign(float s);
    
    int main()
    {
    	float a1, a2, a3, x1, x2, x3, s, q, th, s_sq, q_cub, a1_sq, a1_cub, s_abs;	
    
    
    	printf("The cubic equation has the form x^3 + a1x^2 + a2x + a3 = 0\n");
    	
    	printf("Enter a1: ");
    	scanf("%f", &a1);
    	
    	printf("\nEnter a2: ");
    	scanf("%f", &a2);
    
    	printf("\nEnter a3: ");
    	scanf("%f", &a3);
    
    	a1_cub = pow(a1, 3);
    	a1_sq = pow(a1, 2);
    
    	/* calculate q and s */
    
    	q = ( a1_sq - (3 * a2) ) / 9;
    	
    	s = ( (2 * a1_cub) - (9 * (a1*a2) ) + (27 * a3) ) / 54;
    
    	/* get square / cube of s and q  */
    
    	s_sq = pow(s, 2);
    	q_cub = pow(q, 3);
    
    	s_abs = fabs(s);
    
    
    	/* ****** */
    
    	th = acos( (s/(sqrt(q_cub)) )); /* calculate theta */
    
    	if( (q_cub - s_sq) >= 0)	/* cubic equation has 3 roots */
    	{
    		x1 = ((-2)*(sqrt(q))*(th/3)) - (a1/3);
    		
    		x2 = ((-2)*(sqrt(q))*((th + (2*pi))/3)) - (a1/3);
    
    		x3 = ((-2)*(sqrt(q))*((th + (4*pi))/3)) - (a1/3);
    
    		printf("\nx1 = %f\nx2 = %f\nx3 = %f\n", x1, x2, x3);
    
    	}
    	else if( (s_sq - q_cub) > 0 ) /* cubic equation has 1 root */
    	{
    		
    		float part1 = sqrt(s_sq - q_cub) + s_abs;
    
    		x1 = -(sign(s)) * ( (pow(part1, (1/3))) + (q/(pow(part1, (1/3)))) ) - (a3/3);
    		                                                                                                                                           
    		printf("\nx1 = %f", x1);
    		
    	}
    	else
    	{
    		printf("something crap happened\n");
    	}
    
    
    	system("pause");
    	
    	return 0;		
    }
    
    int sign(float s)
    {
    	if(s > 0)
    	{
    		return 1;
    	}
    	else if(s < 0)
    	{
    		return -1;
    	}
    	
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Any example input, output, and expected output values? One potential problem for the single root solution is that 1/3 will be evaluated as 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    im testing my results with the results from this website

    http://www.agolda.com/CubicEquation.html

    which seem to be accurate

    I changed the (1/3)'s to (0.334)'s and the results are still wrong

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > (1/3)
    This sub-expression will be evaluated as integers, giving 0

    > int sign(float s)
    What does this return when s == 0 ?

    > x1 = ((-2)*(sqrt(q))*(th/3)) - (a1/3);
    Your graphic contains a cos() as well
    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
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    > x1 = ((-2)*(sqrt(q))*(th/3)) - (a1/3);
    Your graphic contains a cos() as well

    ARGH! Thanks...that was a really stupid mistake, a pretty much rewrote the code like 3 times trying to fix it

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Did that fix it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    it worked for a few but when I tried

    a1 = -4
    a2 = -1
    a3 = -20

    I get an incorrect result

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    x1 = -(whatever) ... a1 => a1 = -(x1 whatever...)

    are they the right numbers, but with the wrong signs?

  9. #9
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I assume theyre right...i just followed the information i was given (that image i attacthed)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  2. why does my program print off the wrong answers?
    By kl3pt0 in forum C Programming
    Replies: 18
    Last Post: 06-10-2004, 06:52 PM
  3. I need comments and answers for this C program
    By BadProgrammer in forum C Programming
    Replies: 2
    Last Post: 05-25-2003, 10:57 PM
  4. why different answers for this equation
    By james_nkh in forum C Programming
    Replies: 2
    Last Post: 05-21-2002, 12:29 PM
  5. Replies: 1
    Last Post: 03-12-2002, 06:31 AM