Click here to Skip to main content
11,484,162 members (70,666 online)
Click here to Skip to main content

Tagged as

A simple program to solve quadratic equations with

, 10 Nov 2010 CPOL 4.8K 2
Rate this:
Please Sign up or sign in to vote.
The correct way to solve quadratic equations is none of the above. For the correct algorithm as applied in the following function, seenumerical recipes in C and the Wolfram site at http://mathworld.wolfram.com/QuadraticEquation.html#include bool quadratic(double a, double b,...
The correct way to solve quadratic equations is none of the above. For the correct algorithm as applied in the following function, see
numerical recipes in C and the Wolfram site at http://mathworld.wolfram.com/QuadraticEquation.html

#include <limits>
bool quadratic(double a, double b, double c, double& x1, double& x2, double y, double z)
{
   double delta=0.0e+00, q=0.0e+00, bsign, y1, y2;
   const double eps = std::numeric_limits()<double>.epsilon();
   // equation is: a*x^2 + b*x + c = 0; a cannot be zero.
   if ( fabs(a) <= eps ) { return false; }
 
   delta=b*b-4.0*a*c;
   if ( delta<0.0e+00 ) { return false; }
   if ( delta>=0.0e+00 && delta <= eps )
   { x1=(-1.0*b/(2.0*a)); x2=x1; return true; }
 
   bsign=1.0e+00;
   if ( fabs(b)>1.0e-16 )  { bsign=b/fabs(b); }
   else { if ( b<0.0e+00 ) { bsign=-1.0e+00; } }
 
   q=(-0.5)*(b+bsign*sqrt(delta));
 
   // the roots of the equation are:
   y1=q/a; y2=c/q;
 
   // find if any of the roots is in the given [y,z] interval
   if (( y<=y1 )&&( y1<=z ))
   {
      x1=y1; x2=y2;
   }
   else if (( y<=y2 )&&( y2<=z ))
   {
      x1=y2; x2=y1;
   }
   else
   {
      x1=y1; x2=y2;
   }
 
   return true;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

federico.strati
Software Developer (Senior)
Italy Italy
Senior Software Developer in C/C++ and Oracle.
Ex-physicist holding a Ph.D. on x-ray lasers.

Comments and Discussions

 
GeneralI edited my answser to address your objection. evviva :) Pin
Alain Rist16-Nov-10 8:17
memberAlain Rist16-Nov-10 8:17 
GeneralOh well, thanks Alain, I didn't know the standard evoluted t... Pin
federico.strati10-Nov-10 0:07
memberfederico.strati10-Nov-10 0:07 
General#include &lt;limits&gt; const double eps = std::numeric_limi... Pin
Alain Rist9-Nov-10 23:34
memberAlain Rist9-Nov-10 23:34 
GeneralOh well, the machine epsilon is around 3x10-8 for single pre... Pin
federico.strati9-Nov-10 5:38
memberfederico.strati9-Nov-10 5:38 
GeneralReason for my vote of 2 Aren't the tolerance values arbitrar... Pin
YvesDaoust9-Nov-10 0:30
memberYvesDaoust9-Nov-10 0:30 
GeneralThe inputs "y" and "z" serve the only purpose of finding a r... Pin
federico.strati8-Nov-10 23:23
memberfederico.strati8-Nov-10 23:23 
GeneralCan you elaborate on the function of the 2 added inputs y an... Pin
bstrack8-Nov-10 22:27
memberbstrack8-Nov-10 22:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 10 Nov 2010
Article Copyright 2010 by federico.strati
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid