Skip to content

Commit

Permalink
Уравнения с двумя корнями
Browse files Browse the repository at this point in the history
  • Loading branch information
stden committed May 23, 2015
1 parent bf5c184 commit 9a243cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/QuadraticEquation.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class QuadraticEquation {
* @return корни уравнения (значения x)
*/
public static double[] solve(double a, double b, double c) {
return new double[]{-b / (2 * a)};
// Дискриминант
double D = Math.pow(b, 2) - 4 * a * c;
if (D == 0)
return new double[]{-b / (2 * a)};
double d = Math.sqrt(D);
return new double[]{(-b - d) / (2 * a), (-b + d) / (2 * a)};
}
}
8 changes: 8 additions & 0 deletions src/test/java/QuadraticEquationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ public void testSimpleOneRoot() {
assertArrayEquals("(x-1)^2 = x^2-2x+1", new double[]{1}, QuadraticEquation.solve(1, -2, 1), EPS);
assertArrayEquals("2(x-1)^2 = 2x^2-4x+2", new double[]{1}, QuadraticEquation.solve(2, -4, 2), EPS);
}

/**
* Уравнения с двумя корнями
*/
@Test
public void testTwoSolutions() {
assertArrayEquals("(x-1)(x-2) = x^2-3x+2", new double[]{1, 2}, QuadraticEquation.solve(1, -3, 2), EPS);
}
}

0 comments on commit 9a243cb

Please sign in to comment.