Skip to content

Instantly share code, notes, and snippets.

@ndczz
Created November 21, 2017 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndczz/8a48253bb21d2f4fd47e42e69b930788 to your computer and use it in GitHub Desktop.
Save ndczz/8a48253bb21d2f4fd47e42e69b930788 to your computer and use it in GitHub Desktop.
QuadroSolver
class QuadraticSolver {
public static final int RESULT_BAD = 0;
public static final int RESULT_ONE = 1;
public static final int RESULT_TWO = 2;
public static final Result solve(double a, double b, double c) {
double d = b * b - 4 * a * c;
if (d < 0)
return new Result(RESULT_BAD, 0, 0);
double sqd = Math.sqrt(d);
double root1 = (-b + sqd) / (2 * a);
double root2 = (-b - sqd) / (2 * a);
return new Result(RESULT_TWO, root1, root2);
}
public static class Result {
public int status;
public double first;
public double second;
public Result(int status, double first, double second) {
this.status = status;
this.first = first;
this.second = second;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment