Subject: bug#25255: Emacs calculator evalutes lcm to a negative number

From: [email-masked] (Peder O. Klingenberg)
Date: Sun, 05 Feb 2017 11:28:17 +0100

Bug Poster <[email-masked]> writes:

> This is formula of Least Common Multiple:
>
> lcm(a, b) = |a * b| / gcd(a, b)

I agree, every textbook I can find defines lcm to return a positive
integer.  Here's a patch, which hopefully someone will apply.

>From de52ee092bc0ab7c94d9270b9c3160c0923a68c0 Mon Sep 17 00:00:00 2001
From: "Peder O. Klingenberg" <[email-masked]>
Date: Sun, 5 Feb 2017 10:58:00 +0100
Subject: [PATCH] LCM is defined to return a positive integer.

Bug#25255:
* doc/misc/calc.texi (Combinatorial Functions): The product of
LCM and GCD is an absolute value.
* lisp/calc/calc-comb.el (calcFunc-lcm): Use absolute values.
---
 doc/misc/calc.texi     | 4 ++--
 lisp/calc/calc-comb.el | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/doc/misc/calc.texi b/doc/misc/calc.texi
index baf46f7170..7bd060189c 100644
--- a/doc/misc/calc.texi
+++ b/doc/misc/calc.texi
@@ -19111,8 +19111,8 @@ Combinatorial Functions
 @tindex lcm
 The @kbd{k l} (@code{calc-lcm}) [@code{lcm}] command computes the
 Least Common Multiple of two integers or fractions.  The product of
-the LCM and GCD of two numbers is equal to the product of the
-numbers.
+the LCM and GCD of two numbers is equal to the absolute value of the
+product of the numbers.
 
 @kindex k E
 @pindex calc-extended-gcd
diff --git a/lisp/calc/calc-comb.el b/lisp/calc/calc-comb.el
index bc1ac315a0..c84ff23685 100644
--- a/lisp/calc/calc-comb.el
+++ b/lisp/calc/calc-comb.el
@@ -260,7 +260,7 @@ calcFunc-gcd
 (defun calcFunc-lcm (a b)
   (let ((g (calcFunc-gcd a b)))
     (if (Math-numberp g)
-	(math-div (math-mul a b) g)
+	(math-div (math-abs (math-mul a b)) g)
       (list 'calcFunc-lcm a b))))
 
 (defun calcFunc-egcd (a b)   ; Knuth section 4.5.2
-- 
2.11.0

-- 
...Peder...