blob: 51a7569609941c4dca5156434cd183cf0c76a14b [file] [log] [blame]
caryclark@google.com27accef2012-01-25 18:57:23 +00001#include "QuadraticUtilities.h"
caryclark@google.comd88e0892012-03-27 13:23:51 +00002#include <math.h>
caryclark@google.com27accef2012-01-25 18:57:23 +00003
4int quadraticRoots(double A, double B, double C, double t[2]) {
5 B *= 2;
6 double square = B * B - 4 * A * C;
7 if (square < 0) {
8 return 0;
9 }
10 double squareRt = sqrt(square);
11 double Q = (B + (B < 0 ? -squareRt : squareRt)) / -2;
12 int foundRoots = 0;
13 if ((Q <= A) ^ (Q < 0)) {
14 t[foundRoots++] = Q / A;
15 }
16 if ((C <= Q) ^ (C < 0)) {
17 t[foundRoots++] = C / Q;
18 }
19 return foundRoots;
20}
21
22