blob: ea4cd851a73bde2284a6df360ebc59eb374ed714 [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;
caryclark@google.com78e17132012-04-17 11:40:34 +000012 double ratio;
caryclark@google.com27accef2012-01-25 18:57:23 +000013 int foundRoots = 0;
14 if ((Q <= A) ^ (Q < 0)) {
caryclark@google.com78e17132012-04-17 11:40:34 +000015 ratio = Q / A;
16 if (!isnan(ratio)) {
17 t[foundRoots++] = ratio;
18 }
caryclark@google.com27accef2012-01-25 18:57:23 +000019 }
20 if ((C <= Q) ^ (C < 0)) {
caryclark@google.com78e17132012-04-17 11:40:34 +000021 ratio = C / Q;
22 if (!isnan(ratio)) {
23 t[foundRoots++] = ratio;
24 }
caryclark@google.com27accef2012-01-25 18:57:23 +000025 }
26 return foundRoots;
27}
28
29