2012-01-25 18:57:23 +00:00
|
|
|
#include "QuadraticUtilities.h"
|
2012-03-27 13:23:51 +00:00
|
|
|
#include <math.h>
|
2012-01-25 18:57:23 +00:00
|
|
|
|
|
|
|
int quadraticRoots(double A, double B, double C, double t[2]) {
|
|
|
|
B *= 2;
|
|
|
|
double square = B * B - 4 * A * C;
|
|
|
|
if (square < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
double squareRt = sqrt(square);
|
|
|
|
double Q = (B + (B < 0 ? -squareRt : squareRt)) / -2;
|
|
|
|
int foundRoots = 0;
|
|
|
|
if ((Q <= A) ^ (Q < 0)) {
|
|
|
|
t[foundRoots++] = Q / A;
|
|
|
|
}
|
|
|
|
if ((C <= Q) ^ (C < 0)) {
|
|
|
|
t[foundRoots++] = C / Q;
|
|
|
|
}
|
|
|
|
return foundRoots;
|
|
|
|
}
|
|
|
|
|
|
|
|
|