From dbe33ca873811837c09d3022a1572196f75947f1 Mon Sep 17 00:00:00 2001 From: Derek Atkins Date: Tue, 21 Jan 2003 01:50:16 +0000 Subject: [PATCH] * src/engine/gnc-numeric. -- fix the gnc_numeric_lcd() algo to actually work with numbers that are not co-divisible but have multiple-powers of co-factors. For example, the old algorithm thought the LCM of 100,96875 was 3100, when it is really 387500, because it was removing the factor of '5' too many times.. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@7864 57a11ea4-9604-0410-9ed3-97b8803252fd --- ChangeLog | 6 ++++++ src/engine/gnc-numeric.c | 20 ++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 35fc89244f..fb693c208e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,12 @@ So, let's use g_list_prepend() and g_list_reverse() to speed up the process significantly. + * src/engine/gnc-numeric. -- fix the gnc_numeric_lcd() algo to + actually work with numbers that are not co-divisible but have + multiple-powers of co-factors. For example, the old algorithm + thought the LCM of 100,96875 was 3100, when it is really 387500, + because it was removing the factor of '5' too many times.. + 2003-01-19 John Pierce * doc/Makefile.am diff --git a/src/engine/gnc-numeric.c b/src/engine/gnc-numeric.c index 1333fcd6bd..d55941895c 100644 --- a/src/engine/gnc-numeric.c +++ b/src/engine/gnc-numeric.c @@ -700,15 +700,22 @@ gnc_numeric_lcd(gnc_numeric a, gnc_numeric b) { max_square = small_denom; - /* the LCM algorithm : take the union of the prime factors of the - * two args and multiply them together. To do this, we find the - * successive prime factors of the smaller denominator and eliminate - * them from the larger denominator, then multiply the smaller by - * the remains of the larger. */ + /* the LCM algorithm : factor out the union of the prime factors of the + * two args and then multiply the remainders together. + * + * To do this, we find the successive prime factors of the smaller + * denominator and eliminate them from both the smaller and larger + * denominator (so we only count factors on a one-on-one basis), + * then multiply the original smaller by the remains of the larger. + * + * I.e. LCM 100,96875 == 2*2*5*5,31*5*5*5*5 = 2*2,31*5*5 + * answer: multiply 100 by 31*5*5 == 387500 + */ while(current_divisor * current_divisor <= max_square) { if(((small_denom % current_divisor) == 0) && ((big_denom % current_divisor) == 0)) { big_denom = big_denom / current_divisor; + small_denom = small_denom / current_divisor; } else { if(current_divisor == 2) { @@ -730,7 +737,8 @@ gnc_numeric_lcd(gnc_numeric a, gnc_numeric b) { } } - return small_denom * big_denom; + /* max_sqaure is the original small_denom */ + return max_square * big_denom; }