Quadratic Equation Solver - C++ Sub-routine

Updated DavidB 1 Tallied Votes 727 Views Share

Here is my first Code Snippet contribution: a C++ sub-routine that computes the roots of a quadratic equation.

Notes:

  • This sub-routine checks if a = 0.
  • IF a = 0, then the equation is actually not a quadratic equation; it is a linear equation with one--if any--root.
  • IF the user misses the output statement alerting the user to this fact, the second root has been explicitly assigend a NaN value so that the result is not mistaken for a number.
void Poly2Solver(double a, double b1, double c, double* sr, double* si, double* lr, double* li) {
     // Calculates the zeros of the quadratic a*x^2 + b1*x + c
     // This solver computes both real only AND complex roots (if applicable).
     // The components of the two roots are lr and li (the real and imaginary components, respectively, of one root), and
     // sr and si (the real and imaginary components, respectively,  of the other root).
    
     char rflag;
     double b, d, e;
    
     *sr = *lr = *si = *li = std::numeric_limits<double>::quiet_NaN();
     if (a == 0) {
    	 cout << "\n a = 0. NOT A QUADRATIC EQUATION! TWO ROOTS WILL NOT BE FOUND. \n";
    	 cout << "\nEnter any key to continue. \n";
         cin >> rflag;
    	 if (b1 != 0) {
    		 *sr = -(c/b1);
    		 *si = 0.0;
    	 }
     }
    
     else {
    	*sr = *si = *li = 0.0;
    	if (c == 0){
    		 *lr = -(b1/a);
    	}
    	else {
    		 b = b1/2.0;
    	     if (fabs(b) < fabs(c)){
    		    e = ((c >= 0) ? a : -a);
    		    e = -e + b*(b/fabs(c));
    		    d = sqrt(fabs(e))*sqrt(fabs(c));
    	     } // End if (fabs(b) < fabs(c))
    	     else { // Else (fabs(b) >= fabs(c))
    		    e = -((a/b)*(c/b)) + 1.0;
    		    d = sqrt(fabs(e))*(fabs(b));
    	     } // End else (fabs(b) >= fabs(c))
    
    	     if (e >= 0) { // Real zeros
    		   d = ((b >= 0) ? -d : d);
    		   *lr = (-b + d)/a;
    		   *sr = ((*lr != 0) ? (c/(*lr))/a : *sr);
    	     } // End if (e >= 0)
    	     else { // Complex conjugate zeros
    		   *lr = *sr = -(b/a);
    		   *si = fabs(d/a);
    		   *li = -(*si);
    	     } // End else (e < 0)
    	}
     }
     return;
     } // End Poly2Solver
sanjulovers -3 Light Poster

can i suggest
a more simpler version ( but a bit long) version of the same function?????

DavidB 44 Junior Poster

If you are planning to submit your own code snippet, you are welcome to do so.
But I would suggest you create your own thread for it, within the "Code Snippet" section.

sanjulovers -3 Light Poster

I thought it would be easier another way. i.e using only basic elements of c++

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

David, it seems that your < and > have been turned into &lt; and &gt; respectively. Did you post it that way? If not, I'll have to ask Dani to look into it.

DavidB 44 Junior Poster

David, it seems that your < and > have been turned into &lt; and &gt; respectively. Did you post it that way? If not, I'll have to ask Dani to look into it.

No, I did not post it that way. If you hadn't pointed it out, I wouldn't have even noticed it. Thanks for catching it.

Yes, if an admin could edit the code to put the braces back in there, that would be good.

DavidB 44 Junior Poster

David, it seems that your < and > have been turned into &lt; and &gt; respectively. Did you post it that way? If not, I'll have to ask Dani to look into it.

Oops, I just noticed a few more errors:

Quotation marks (") in the cout statement have been replaced by &quot;
and the newline character (\n) has been replaced by an 'n' only--the backslash in front of it has been deleted.

Hmmnn ... I wonder why that happened? I thought anything wrapped in code tags would be posted as-is (i.e. - nothing converted to HTML, formatted, etc.).

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Can you post a clean version again so that I can update the original snippet? It would be too much work editing the original snippet.

Also, if you didn't post it that way, this is something for Dani to look into.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Actually iamthwee brought it to our notice in this thread he created.

Nutster 58 Newbie Poster

You are assuming all your pointers are valid. You should check for NULL pointers before assigning to pointer targets. I would recommend using references to return the results instead of pointers. Otherwise, a nice idea.

DavidB 44 Junior Poster
 void Poly2Solver(double a, double b1, double c, double* sr, double* si, double* lr, double* li) {
 // Calculates the zeros of the quadratic a*x^2 + b1*x + c
 // This solver computes both real only AND complex roots (if applicable).
 // The components of the two roots are lr and li (the real and imaginary components, respectively, of one root), and
 // sr and si (the real and imaginary components, respectively,  of the other root).

 char rflag;
 double b, d, e;

 *sr = *lr = *si = *li = std::numeric_limits<double>::quiet_NaN();
 if (a == 0) {
     cout << "\n a = 0. NOT A QUADRATIC EQUATION! TWO ROOTS WILL NOT BE FOUND. \n";
     cout << "\nEnter any key to continue. \n";
     cin >> rflag;
     if (b1 != 0) {
         *sr = -(c/b1);
         *si = 0.0;
     }
 }

 else {
    *sr = *si = *li = 0.0;
    if (c == 0){
         *lr = -(b1/a);
    }
    else {
         b = b1/2.0;
         if (fabs(b) < fabs(c)){
            e = ((c >= 0) ? a : -a);
            e = -e + b*(b/fabs(c));
            d = sqrt(fabs(e))*sqrt(fabs(c));
         } // End if (fabs(b) < fabs(c))
         else { // Else (fabs(b) >= fabs(c))
            e = -((a/b)*(c/b)) + 1.0;
            d = sqrt(fabs(e))*(fabs(b));
         } // End else (fabs(b) >= fabs(c))

         if (e >= 0) { // Real zeros
           d = ((b >= 0) ? -d : d);
           *lr = (-b + d)/a;
           *sr = ((*lr != 0) ? (c/(*lr))/a : *sr);
         } // End if (e >= 0)
         else { // Complex conjugate zeros
           *lr = *sr = -(b/a);
           *si = fabs(d/a);
           *li = -(*si);
         } // End else (e < 0)
    }
 }
 return;
 } // End Poly2Solver
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.