home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / C-SSP.ARJ / QUADRT.C < prev    next >
Encoding:
Text File  |  1984-07-17  |  972 b   |  49 lines

  1.     quadrt(a,root1,root2,tol,numrt)
  2.  
  3.       /* this function solves any quadratic equation of the form */
  4.       /* a(0)x**2 + a(1)x + a(2) for real or imaginary roots.*/
  5.  
  6.       int *numrt;
  7.       float a[],root1[],root2[],tol;
  8.  
  9.     {
  10.  
  11.       float w,x,y,z,zero;
  12.       extern double fabs(),sqrt();
  13.  
  14.       zero = tol/10.;
  15.  
  16.       if((fabs(a[0])-zero) <= 0.)
  17.       {
  18.        if((fabs(a[1])-zero) <= 0.)
  19.        {
  20.         *numrt = 0;
  21.         return;
  22.        }
  23.        *numrt = 1;
  24.        root1[0] = -a[2]/a[1];
  25.        root1[1] = 0.;
  26.        return;
  27.       }
  28.       *numrt = 2;
  29.       x = (a[1] * a[1]) - 4.0 * a[0] * a[2];
  30.       y = a[0] + a[0];
  31.       z = sqrt(fabs(x))/y;
  32.       w = -a[1]/y;
  33.  
  34.       if(x >= 0.0)
  35.       {
  36.        root1[0] = w + z;
  37.        root1[1] = 0.0;
  38.        root2[0] = w - z;
  39.        root2[1] = 0.0;
  40.        return;
  41.       }
  42.       root1[0] = w;
  43.       root1[1] = z;
  44.       root2[0] = w;
  45.       root2[1] = -z;
  46.       return;
  47.    }
  48.  
  49.