Repository: TheAlgorithms/C-Plus-Plus
File: math/quadratic_equations_complex_numbers.cpp (C++)

Code before:
    }

    long double discriminant = b * b - 4 * a * c;
    std::array<std::complex<long double>, 2> solutions{0, 0};

    // Complex root (discriminant < 0)
    // Note that the left term (-b / 2a) is always real. The imaginary part
    // appears when b^2 - 4ac < 0, so sqrt(b^2 - 4ac) has no real roots. So,
    // the imaginary component is i * (+/-)sqrt(abs(b^2 - 4ac)) / 2a.
    if (discriminant < 0) {
        // Since b^2 - 4ac is < 0, for faster computation, -discriminant is
        // enough to make it positive.
        solutions[0] = std::complex<long double>{
            -b * 0.5 / a, -std::sqrt(-discriminant) * 0.5 / a};
        solutions[1] = std::complex<long double>{
            -b * 0.5 / a, std::sqrt(-discriminant) * 0.5 / a};
    } else {
        // Since discriminant > 0, there are only real roots. Therefore,
        // imaginary component = 0.
        solutions[0] = std::complex<long double>{
            (-b - std::sqrt(discriminant)) * 0.5 / a, 0};
        solutions[1] = std::complex<long double>{
            (-b + std::sqrt(discriminant)) * 0.5 / a, 0};
    }

    return solutions;
}

}  // namespace math

/**
 * @brief Asserts an array of complex numbers.
 * @param input Input array of complex numbers. .
 * @param expected Expected array of complex numbers.
 * @param precision Precision to be asserted. Default=10
 */
void assertArray(std::array<std::complex<long double>, 2> input,
                 std::array<std::complex<long double>, 2> expected,
                 size_t precision = 10) {
    long double exponent = std::pow(10, precision);
    input[0].real(std::round(input[0].real() * exponent));
    input[1].real(std::round(input[1].real() * exponent));
    input[0].imag(std::round(input[0].imag() * exponent));
    input[1].imag(std::round(input[1].imag() * exponent));

    expected[0].real(std::round(expected[0].real() * exponent));
    expected[1].real(std::round(expected[1].real() * exponent));
    expected[0].imag(std::round(expected[0].imag() * exponent));
    expected[1].imag(std::round(expected[1].imag() * exponent));

    assert(input == expected);

Review comment (suggestion):
Also non-blocking comment. It might be better to get rid of that else entirely - but that's just a preference here

```suggestion
    if (discriminant < 0) {
        // Since b^2 - 4ac is < 0, for faster computation, -discriminant is
        // enough to make it positive.
        solutions[0] = std::complex<long double>{
            -b * 0.5 / a, -std::sqrt(-discriminant) * 0.5 / a};
        solutions[1] = std::complex<long double>{
            -b * 0.5 / a, std::sqrt(-discriminant) * 0.5 / a};
        return solutions;
    }
    // Since discriminant > 0, there are only real roots. Therefore,
    // imaginary component = 0.
    solutions[0] = std::complex<long double>{
        (-b - std::sqrt(discriminant)) * 0.5 / a, 0};
    solutions[1] = std::complex<long double>{
        (-b + std::sqrt(discriminant)) * 0.5 / a, 0};
    return solutions;
```

Code after:
    }

    long double discriminant = b * b - 4 * a * c;
    std::array<std::complex<long double>, 2> solutions{0, 0};

    if (discriminant == 0) {
        solutions[0] = -b * 0.5 / a;
        solutions[1] = -b * 0.5 / a;
        return solutions;
    }
    
    // Complex root (discriminant < 0)
    // Note that the left term (-b / 2a) is always real. The imaginary part
    // appears when b^2 - 4ac < 0, so sqrt(b^2 - 4ac) has no real roots. So,
    // the imaginary component is i * (+/-)sqrt(abs(b^2 - 4ac)) / 2a.
    if (discriminant > 0) {
        // Since discriminant > 0, there are only real roots. Therefore,
        // imaginary component = 0.
        solutions[0] = std::complex<long double>{
            (-b - std::sqrt(discriminant)) * 0.5 / a, 0};
        solutions[1] = std::complex<long double>{
            (-b + std::sqrt(discriminant)) * 0.5 / a, 0};
        return solutions;
    }
    // Since b^2 - 4ac is < 0, for faster computation, -discriminant is
    // enough to make it positive.
    solutions[0] = std::complex<long double>{
        -b * 0.5 / a, -std::sqrt(-discriminant) * 0.5 / a};
    solutions[1] = std::complex<long double>{
        -b * 0.5 / a, std::sqrt(-discriminant) * 0.5 / a};

    return solutions;
}

}  // namespace math

/**
 * @brief Asserts an array of complex numbers.
 * @param input Input array of complex numbers. .
 * @param expected Expected array of complex numbers.
 * @param precision Precision to be asserted. Default=10
 */
void assertArray(std::array<std::complex<long double>, 2> input,
                 std::array<std::complex<long double>, 2> expected,
                 size_t precision = 10) {
    long double exponent = std::pow(10, precision);
    input[0].real(std::round(input[0].real() * exponent));
    input[1].real(std::round(input[1].real() * exponent));
    input[0].imag(std::round(input[0].imag() * exponent));
    input[1].imag(std::round(input[1].imag() * exponent));