static int iGetRoots(double abc[], double xx[]) // Returns real roots of a quadratic form // M.Lampton UCB SSL 2003, 2011 { double a = abc[0]; double b = abc[1]; double c = abc[2]; xx[0] = xx[1] = -0.0; if (a == 0.0) { if (b == 0.0) return 0; xx[0] = xx[1] = -c/b; return 1; } if (c == 0.0) { xx[0] = 0.0; xx[1] = -b/a; return 2; } if (b == 0.0) { if (c/a > 0.0) return 0; xx[0]= Math.sqrt(-c/a); xx[1] = -xx[0]; return 2; } //-------abc all nonzero----- double discrim = b*b-4.0*a*c; if (discrim < 0.0) return 0; if (discrim == 0.0) { xx[0] = xx[1] = -b/(2*a); return 2; } double root = Math.sqrt(discrim); double q = (b>0.0) ? (b+root)/2 : (b-root)/2; xx[0] = -c/q; xx[1] = -q/a; return 2; }