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 |
|
14 |
#include <math.h> |
15 |
|
16 |
#include "fvect.h" |
17 |
|
18 |
|
19 |
int |
20 |
quadratic(r, a, b, c) /* find real roots of quadratic equation */ |
21 |
double *r; /* roots in ascending order */ |
22 |
double a, b, c; |
23 |
{ |
24 |
double disc; |
25 |
int first; |
26 |
|
27 |
if (a < -FTINY) |
28 |
first = 1; |
29 |
else if (a > FTINY) |
30 |
first = 0; |
31 |
else if (fabs(b) > FTINY) { /* solve linearly */ |
32 |
r[0] = -c/b; |
33 |
return(1); |
34 |
} else |
35 |
return(0); /* equation is c == 0 ! */ |
36 |
|
37 |
b *= 0.5; /* simplifies formula */ |
38 |
|
39 |
disc = b*b - a*c; /* discriminant */ |
40 |
|
41 |
if (disc < -FTINY*FTINY) /* no real roots */ |
42 |
return(0); |
43 |
|
44 |
if (disc <= FTINY*FTINY) { /* double root */ |
45 |
r[0] = -b/a; |
46 |
return(1); |
47 |
} |
48 |
|
49 |
disc = sqrt(disc); |
50 |
|
51 |
r[first] = (-b - disc)/a; |
52 |
r[1-first] = (-b + disc)/a; |
53 |
|
54 |
return(2); |
55 |
} |