Repository: opencv/opencv
File: modules/core/test/test_math.cpp (C++)

Code before:
        EXPECT_EQ(roots[0], -5e12 - 2.);
    }
}

TEST(Core_SolveCubic, regression_27748)
{
    // a is extremely small relative to others (approx 1.8e-19 ratio),
    // causing instability in standard cubic formula.
    double a = 1.56041e-17;
    double b = 84.4504;
    double c = -96.795;
    double d = 13.6826;

    Mat coeffs = (Mat_<double>(1, 4) << a, b, c, d);
    Mat roots;

    int n = solveCubic(coeffs, roots);

    // Expecting quadratic behavior (2 roots)
    EXPECT_GE(n, 2);

    // Verify roots satisfy the quadratic part of the equation (since a*x^3 is negligible)
    for(int i = 0; i < n; i++)
    {
        double x = roots.at<double>(i);
        double val = b*x*x + c*x + d;
        // Check residual is small
        EXPECT_LE(fabs(val), 1e-6) << "Root " << x << " does not satisfy the equation";
    }
}

TEST(Core_SolvePoly, regression_5599)
{
    // x^4 - x^2 = 0, roots: 1, -1, 0, 0
    cv::Mat coefs = (cv::Mat_<float>(1,5) << 0, 0, -1, 0, 1 );
    {
        cv::Mat r;
        double prec;
        prec = cv::solvePoly(coefs, r);
        EXPECT_LE(prec, 1e-6);
        EXPECT_EQ(4u, r.total());
        //std::cout << "Preciseness = " << prec << std::endl;
        //std::cout << "roots:\n" << r << "\n" << std::endl;
        ASSERT_EQ(CV_32FC2, r.type());
        checkRoot<float>(r, 1, 0);
        checkRoot<float>(r, -1, 0);
        checkRoot<float>(r, 0, 0);
        checkRoot<float>(r, 0, 0);
    }
    // x^2 - 2x + 1 = 0,  roots: 1, 1
    coefs = (cv::Mat_<float>(1,3) << 1, -2, 1 );

Review comment (suggestion):
IMHO we should include cubic part too. The minimal impact of it is solver assumption, but not requirement. 

Code after:
        EXPECT_EQ(roots[0], -5e12 - 2.);
    }
}

TEST(Core_SolveCubic, regression_27748)
{
    // a is extremely small relative to others (approx 1.8e-19 ratio),
    // causing instability in standard cubic formula.
    double a = 1.56041e-17;
    double b = 84.4504;
    double c = -96.795;
    double d = 13.6826;

    Mat coeffs = (Mat_<double>(1, 4) << a, b, c, d);
    Mat roots;

    int n = solveCubic(coeffs, roots);

    // Expecting quadratic behavior (2 roots)
    EXPECT_GE(n, 2);

    // Verify roots satisfy the FULL cubic equation
    for(int i = 0; i < n; i++)
    {
        double x = roots.at<double>(i);
        double val = a*x*x*x + b*x*x + c*x + d;
        // Check residual is small
        EXPECT_LE(fabs(val), 1e-6) << "Root " << x << " does not satisfy the equation";
    }
}

TEST(Core_SolvePoly, regression_5599)
{
    // x^4 - x^2 = 0, roots: 1, -1, 0, 0
    cv::Mat coefs = (cv::Mat_<float>(1,5) << 0, 0, -1, 0, 1 );
    {
        cv::Mat r;
        double prec;
        prec = cv::solvePoly(coefs, r);
        EXPECT_LE(prec, 1e-6);
        EXPECT_EQ(4u, r.total());
        //std::cout << "Preciseness = " << prec << std::endl;
        //std::cout << "roots:\n" << r << "\n" << std::endl;
        ASSERT_EQ(CV_32FC2, r.type());
        checkRoot<float>(r, 1, 0);
        checkRoot<float>(r, -1, 0);
        checkRoot<float>(r, 0, 0);
        checkRoot<float>(r, 0, 0);
    }
    // x^2 - 2x + 1 = 0,  roots: 1, 1
    coefs = (cv::Mat_<float>(1,3) << 1, -2, 1 );