diff --git a/src/main/java/QuadraticEquation.java b/src/main/java/QuadraticEquation.java index 5f80cdb..34a7272 100644 --- a/src/main/java/QuadraticEquation.java +++ b/src/main/java/QuadraticEquation.java @@ -17,6 +17,8 @@ public static double[] solve(double a, double b, double c) { double D = Math.pow(b, 2) - 4 * a * c; if (Math.abs(D) < EPS) return new double[]{-b / (2 * a)}; + if (D < 0) + return new double[]{}; double d = Math.sqrt(D); return new double[]{(-b - d) / (2 * a), (-b + d) / (2 * a)}; } diff --git a/src/test/java/QuadraticEquationTest.java b/src/test/java/QuadraticEquationTest.java index 9007ad1..e6397c1 100644 --- a/src/test/java/QuadraticEquationTest.java +++ b/src/test/java/QuadraticEquationTest.java @@ -35,4 +35,12 @@ public void testOneRootAccuracy() { assertArrayEquals("t(x-q)^2 = t*x^2 - 2*t*q*x + t*q^2", new double[]{q}, QuadraticEquation.solve(t, -2 * t * q, t * q * q), EPS); } } + + /** + * Нет решений (корней) + */ + @Test + public void testNoRoots() { + assertArrayEquals("x^2 + 2 = 0", new double[]{}, QuadraticEquation.solve(1, 0, 2), EPS); + } }