| 1 |
greg |
1.1 |
/* Copyright (c) 1986 Regents of the University of California */ |
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* zeroes.c - compute roots for various equations. |
| 9 |
|
|
* |
| 10 |
|
|
* 8/19/85 |
| 11 |
|
|
*/ |
| 12 |
|
|
|
| 13 |
|
|
#define FTINY 1e-7 |
| 14 |
|
|
|
| 15 |
|
|
|
| 16 |
|
|
int |
| 17 |
|
|
quadratic(r, a, b, c) /* find real roots of quadratic equation */ |
| 18 |
|
|
double *r; /* roots in ascending order */ |
| 19 |
|
|
double a, b, c; |
| 20 |
|
|
{ |
| 21 |
|
|
double fabs(), sqrt(); |
| 22 |
|
|
double disc; |
| 23 |
|
|
int first; |
| 24 |
|
|
|
| 25 |
|
|
if (a < -FTINY) |
| 26 |
|
|
first = 1; |
| 27 |
|
|
else if (a > FTINY) |
| 28 |
|
|
first = 0; |
| 29 |
|
|
else if (fabs(b) > FTINY) { /* solve linearly */ |
| 30 |
|
|
r[0] = -c/b; |
| 31 |
|
|
return(1); |
| 32 |
|
|
} else |
| 33 |
|
|
return(0); /* equation is c == 0 ! */ |
| 34 |
|
|
|
| 35 |
|
|
b *= 0.5; /* simplifies formula */ |
| 36 |
|
|
|
| 37 |
|
|
disc = b*b - a*c; /* discriminant */ |
| 38 |
|
|
|
| 39 |
|
|
if (disc < -FTINY*FTINY) /* no real roots */ |
| 40 |
|
|
return(0); |
| 41 |
|
|
|
| 42 |
|
|
if (disc <= FTINY*FTINY) { /* double root */ |
| 43 |
|
|
r[0] = -b/a; |
| 44 |
|
|
return(1); |
| 45 |
|
|
} |
| 46 |
|
|
|
| 47 |
|
|
disc = sqrt(disc); |
| 48 |
|
|
|
| 49 |
|
|
r[first] = (-b - disc)/a; |
| 50 |
|
|
r[1-first] = (-b + disc)/a; |
| 51 |
|
|
|
| 52 |
|
|
return(2); |
| 53 |
|
|
} |