From 402ef4c9cb2f69627ca80d602b928e56056e1a2a Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Sun, 7 Nov 2010 19:23:48 +0000 Subject: [PATCH] correct 2 bugs in cvSolveCubic (linear and quadratic cases: tickets #525 and #364) --- modules/core/src/mathfuncs.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/core/src/mathfuncs.cpp b/modules/core/src/mathfuncs.cpp index 56a06ec..c56312a 100644 --- a/modules/core/src/mathfuncs.cpp +++ b/modules/core/src/mathfuncs.cpp @@ -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; } } -- 2.7.4