11,480,082 members (69,222 online)
Alternative Tip/Trick

# A simple program to solve quadratic equations with

, 10 Nov 2010 CPOL 4.7K 2
 Rate this:
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;
}
```

## Share

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

 First Prev Next
 I edited my answser to address your objection. evviva :) Alain Rist16-Nov-10 8:17 Alain Rist 16-Nov-10 8:17
 Oh well, thanks Alain, I didn't know the standard evoluted t... federico.strati10-Nov-10 0:07 federico.strati 10-Nov-10 0:07
 #include <limits> const double eps = std::numeric_limi... Alain Rist9-Nov-10 23:34 Alain Rist 9-Nov-10 23:34
 Oh well, the machine epsilon is around 3x10-8 for single pre... federico.strati9-Nov-10 5:38 federico.strati 9-Nov-10 5:38
 Oh well, the machine epsilon is around 3x10-8 for single precision and around somethingx10-16 for double precision... but to effectively state the tolerance for each machine you need a list of define's as in the GSL (GNU Scientific Library). It's not given in or other standard includes. Hence the used value 1x10-15 is a good choice for portable programs... It is up to the programmer and left as an exercise for the reader here to implement machine epsilons define's appropriate for its programs. Remember this is just a Tip, not a full bloated library... Cheers
 Reason for my vote of 2 Aren't the tolerance values arbitrar... YvesDaoust9-Nov-10 0:30 YvesDaoust 9-Nov-10 0:30
 The inputs "y" and "z" serve the only purpose of finding a r... federico.strati8-Nov-10 23:23 federico.strati 8-Nov-10 23:23
 Can you elaborate on the function of the 2 added inputs y an... bstrack8-Nov-10 22:27 bstrack 8-Nov-10 22:27
 Last Visit: 31-Dec-99 19:00     Last Update: 23-May-15 2:52 Refresh 1