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