Im trying to write a program to calculate the roots of a cubic using Cardanos formula. Im new to programming and C, i've been at this problem for about 4 hours or so. I struggled to get my head around function prototypes and I dont really think i've worked it out yet.
Firstly, if i omit the A1 = blah line, it compiles, but I get "Q = nan S = nan", i dont understand why its returning "not a number" for these values??
Secondly, i dont understand how to use the pow() operator on the A1 line.
Thirdly, it wont compile because i think i've stuffed up the use of the function. I dont quite understand whats going on.
Fourthly.. should it be int main(), or float main(), the values that are to be calculated could be floating point numbers, not integers.
Fifthly.. I want it all in terms of a = 1, hence my if (a != 1) line, i used an if statement to do this, but then i wasn't sure if i needed an else statement. cant i just remove the else statement? is that redundant? i just want it to test if a is equal to 1, and if it isnt, i want it to divide the other coefficents by a.
Thanks so much for reading this and thanks alot if you post.
float sign(float S); /* sign function prototype */
float Q, S, a, b, c, d, A1, x1, x2, x3, theta;
/* Get coefficients from user */
printf("Cubic coefficient a? ");
printf("Cubic coefficient b? ");
printf("Cubic coefficient c? ");
printf("Cubic coefficient d? ");
/* Make sure a = 1, if it doesn't, recalculate the rest of the coefficents */
if (a != 1)
a = 1;
b = b/a;
c = c/a;
d = d/a;
a = a;
b = b;
c = c;
d = d;
/* Now to do some calculations to be used later in the program */
Q = (b*b - 3.*c)/(9.0);
S = (2.*b*b - 9.*b*c + 27.*d)/(54.0);
A1 = -sign(S)((sqrt[S*S - Q*Q*Q] + fabs(S))^(1/3);
printf("The value of %f = Q, and %f = S is\n" Q, S);
/* calculations will be done here */
/* if Q^3 - S^2 > 0 will be done here */
/* if Q^3 - S^2 <= 0 will be done here */
float sign(float S)
if (S > 0)
else if (S < 0)