[gnumeric] LCM: Respect the "const" for the data.



commit 4634ad0fbeec0cbfb5aa81013b5a73029d87dc52
Author: Morten Welinder <terra gnome org>
Date:   Thu May 14 11:59:14 2009 -0400

    LCM: Respect the "const" for the data.
---
 plugins/fn-math/ChangeLog   |    4 +++
 plugins/fn-math/functions.c |   48 +++++++++++++++++++-----------------------
 2 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/plugins/fn-math/ChangeLog b/plugins/fn-math/ChangeLog
index 532f63f..a53cfe3 100644
--- a/plugins/fn-math/ChangeLog
+++ b/plugins/fn-math/ChangeLog
@@ -1,3 +1,7 @@
+2009-05-14  Morten Welinder  <terra gnome org>
+
+	* functions.c (range_lcm): Don't change the supplied data.
+
 2009-05-06  Morten Welinder <terra gnome org>
 
 	* Release 1.9.7
diff --git a/plugins/fn-math/functions.c b/plugins/fn-math/functions.c
index 5438c55..1fd1abc 100644
--- a/plugins/fn-math/functions.c
+++ b/plugins/fn-math/functions.c
@@ -76,7 +76,7 @@ gnm_gcd (gnm_float a, gnm_float b)
 	g_return_val_if_fail (a > 0 && a <= gnm_gcd_max, -1);
 	g_return_val_if_fail (b > 0 && b <= gnm_gcd_max, -1);
 
-	while (gnm_abs (b) > 0.5) {
+	while (b > 0.5) {
 		gnm_float r = gnm_fmod (a, b);
 		a = b;
 		b = r;
@@ -140,37 +140,33 @@ static GnmFuncHelp const help_lcm[] = {
 	{ GNM_FUNC_HELP_END }
 };
 
+static gnm_float
+gnm_lcm (gnm_float a, gnm_float b)
+{
+	return a * (b / gnm_gcd (a, b));
+}
+
 static int
 range_lcm (gnm_float const *xs, int n, gnm_float *res)
 {
-	/* This function violates the "const".  */
-	gnm_float *xsuc = (gnm_float *)xs;
-
-	if (n > 0) {
-		int i, j;
-		gnm_float gcd_so_far = 1;
-
-		for (i = j = 0; i < n; i++) {
-			int k;
-			gnm_float thisx = gnm_fake_floor (xsuc[i]);
-
-			if (thisx < 1 || thisx > gnm_gcd_max)
-				return 1;
-
-			for (k = 0; k < j; k++)
-				thisx /= gnm_gcd (thisx, xsuc[k]);
+	int i;
+	gnm_float lcm;
 
-			if (thisx == 1)
-				continue;
+	if (n <= 0)
+		return 1;
 
-			xsuc[j++] = thisx;
-			gcd_so_far *= thisx;
-		}
+	lcm = 1;
+	for (i = 0; i < n; i++) {
+		gnm_float thisx = gnm_fake_floor (xs[i]);
+		if (thisx == 1)
+			continue;
+		if (thisx < 1 || thisx > gnm_gcd_max || lcm > gnm_gcd_max)
+			return 1;
+		lcm = gnm_lcm (lcm, thisx);
+	}
 
-		*res = gcd_so_far;
-		return 0;
-	} else
-		return 1;
+	*res = lcm;
+	return 0;
 }
 
 static GnmValue *



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]