[Testdome] quadratic equation

whitehousechef·2023년 12월 6일

just finding the roots and making a roots object from those roots

but if we have 1 same root then we use the same root to make the object

    public static Roots findRoots(double a, double b, double c) {
        double discriminant = b * b - 4 * a * c;

        if (discriminant < 0) {
            // No real roots
            return new Roots(Double.NaN, Double.NaN);
        } else if (discriminant == 0) {
            // One real root
            double x = -b / (2 * a);
            return new Roots(x, x);
        } else {
            // Two real roots
            double x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
            double x2 = (-b - Math.sqrt(discriminant)) / (2 * a);
            return new Roots(x1, x2);
        }
    }

0개의 댓글