avoid divide by zero

R=kjlubick@google.com
Bug: skia:7623
Change-Id: Ia9fb31201a39a4540d94f97e8d2f06eb290ab6e5
Reviewed-on: https://skia-review.googlesource.com/114082
Commit-Queue: Cary Clark <caryclark@skia.org>
Commit-Queue: Cary Clark <caryclark@google.com>
Reviewed-by: Kevin Lubick <kjlubick@google.com>
This commit is contained in:
Cary Clark 2018-03-13 10:39:30 -04:00 committed by Skia Commit-Bot
parent 99b63400eb
commit 65247e595d

View File

@ -141,6 +141,15 @@ int SkDQuad::RootsValidT(double A, double B, double C, double t[2]) {
return foundRoots;
}
static int handle_zero(const double B, const double C, double s[2]) {
if (approximately_zero(B)) {
s[0] = 0;
return C == 0;
}
s[0] = -C / B;
return 1;
}
/*
Numeric Solutions (5.6) suggests to solve the quadratic by computing
Q = -1/2(B + sgn(B)Sqrt(B^2 - 4 A C))
@ -150,16 +159,13 @@ and using the roots
*/
// this does not discard real roots <= 0 or >= 1
int SkDQuad::RootsReal(const double A, const double B, const double C, double s[2]) {
if (!A) {
return handle_zero(B, C, s);
}
const double p = B / (2 * A);
const double q = C / A;
if (!A || (approximately_zero(A) && (approximately_zero_inverse(p)
|| approximately_zero_inverse(q)))) {
if (approximately_zero(B)) {
s[0] = 0;
return C == 0;
}
s[0] = -C / B;
return 1;
if (approximately_zero(A) && (approximately_zero_inverse(p) || approximately_zero_inverse(q))) {
return handle_zero(B, C, s);
}
/* normal form: x^2 + px + q = 0 */
const double p2 = p * p;