correct 2 bugs in cvSolveCubic (linear and quadratic cases: tickets #525 and #364)
authorVadim Pisarevsky <no@email>
Sun, 7 Nov 2010 19:23:48 +0000 (19:23 +0000)
committerVadim Pisarevsky <no@email>
Sun, 7 Nov 2010 19:23:48 +0000 (19:23 +0000)
modules/core/src/mathfuncs.cpp

index 56a06ec..c56312a 100644 (file)
@@ -2285,7 +2285,7 @@ cvSolveCubic( const CvMat* coeffs, CvMat* roots )
             else
             {
                 // linear equation
-                x0 = a3/a2;
+                x0 = -a3/a2;
                 n = 1;
             }
         }
@@ -2296,9 +2296,18 @@ cvSolveCubic( const CvMat* coeffs, CvMat* roots )
             if( d >= 0 )
             {
                 d = sqrt(d);
-                double q = (-a2 + (a2 < 0 ? -d : d)) * 0.5;
-                x0 = q / a1;
-                x1 = a3 / q;
+                double q1 = (-a2 + d) * 0.5;
+                double q2 = (a2 + d) * -0.5;
+                if( fabs(q1) > fabs(q2) )
+                {
+                    x0 = q1 / a1;
+                    x1 = a3 / q1;
+                }
+                else
+                {
+                    x0 = q2 / a1;
+                    x1 = a3 / q2;
+                }
                 n = d > 0 ? 2 : 1;
             }
         }