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 484d1ef commit 74f43e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/QuadraticEquation.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class QuadraticEquation {
* @return корни уравнения (значения x)
*/
public static double[] solve(double a, double b, double c) {
if (Math.abs(a) < EPS) {
return new double[]{-c / b};
}
// Дискриминант
double D = Math.pow(b, 2) - 4 * a * c;
if (Math.abs(D) < EPS)
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/QuadraticEquationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public void testOneRootAccuracy() {
public void testNoRoots() {
assertArrayEquals("x^2 + 2 = 0", new double[]{}, QuadraticEquation.solve(1, 0, 2), EPS);
}

/**
* Линейное уравнение
*/
@Test
public void testLinear() {
assertArrayEquals("3x + 4 = 0", new double[]{-4 / 3.}, QuadraticEquation.solve(0, 3, 4), EPS);
assertArrayEquals("1e-16*x^2 + x - 100 = 0", new double[]{100}, QuadraticEquation.solve(1e-16, 1, -100), EPS);
}
}

0 comments on commit 74f43e6

Please sign in to comment.