changeset 11061:604dfbaf5010

Fix off-by-one and typo bugs in gcd
author Jordi Gutiérrez Hermoso <jordigh@gmail.com>
date Thu, 30 Sep 2010 02:05:30 -0500
parents 54697b37d8bf
children 88d78ee12ee8
files src/ChangeLog src/DLD-FUNCTIONS/gcd.cc
diffstat 2 files changed, 10 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2010-09-30  Jordi GutiƩrrez Hermoso  <jordigh@gmail.com>
+
+	* DLD-FUNCTIONS/gcd.cc (extended_gcd): Fix bug that didn't
+	distinguish the two output coefficients.
+	(Fgcd): Fix off-by-one bug and typo from copy-pasted code.
+
 2010-09-30  Jaroslav Hajek  <highegg@gmail.com>
 
 	* oct-parse.yy (Fautoload): Replace Octave_map by octave_map.
--- a/src/DLD-FUNCTIONS/gcd.cc
+++ b/src/DLD-FUNCTIONS/gcd.cc
@@ -75,7 +75,7 @@
       ("gcd: all values must be integers");
 
   double aa = fabs (a), bb = fabs (b);
-  double xx = 0, lx = 1, yy = 0, ly = 1;
+  double xx = 0, lx = 1, yy = 1, ly = 0;
   while (bb != 0)
     {
       double qq = floor (aa / bb);
@@ -101,7 +101,7 @@
               octave_int<T>& x, octave_int<T>& y)
 {
   T aa = a.abs ().value (), bb = b.abs ().value ();
-  T xx = 0, lx = 1, yy = 0, ly = 1;
+  T xx = 0, lx = 1, yy = 1, ly = 0;
   while (bb != 0)
     {
       T qq = aa / bb;
@@ -350,10 +350,10 @@
           for (int j = 2; j < nargin; j++)
             {
               octave_value x;
-              retval(0) = do_extended_gcd (retval(0), args(1), 
+              retval(0) = do_extended_gcd (retval(0), args(j),
                                            x, retval(j+1));
               for (int i = 0; i < j; i++)
-                retval(i).assign (octave_value::op_el_mul_eq, x);
+                retval(i+1).assign (octave_value::op_el_mul_eq, x);
               if (error_state)
                 break;
             }