2008/8/12 Mikael Djurfeldt <[EMAIL PROTECTED]>:
> 2008/8/12 Neil Jerram <[EMAIL PROTECTED]>:
>> 2008/8/12 Bill Schottstaedt <[EMAIL PROTECTED]>:
>>> gcd is supposed to ignore factors of -1.
>>
>> According to?  (I'm not suggesting that you're wrong.  I'd just like
>> you to be precise about your references.)
>
> R5RS:
>
> 6.2.5 Numerical operations
>
>  -- library procedure: gcd n1 ...,
>  -- library procedure: lcm n1 ...,
>     These procedures return the greatest common divisor or least common
>     multiple of their arguments.  The result is always non-negative.

Patch is attached, for review.

      Neil
From 420a9ff5588852795bb3bbb99f878f0471e8fe58 Mon Sep 17 00:00:00 2001
From: Neil Jerram <[EMAIL PROTECTED]>
Date: Wed, 17 Sep 2008 21:46:40 +0100
Subject: [PATCH] Fix for incorrect (gcd -2) => -2; should give 2.

(reported by Bill Schottstaedt)

* libguile/numbers.c (scm_gcd): When only one arg given, use scm_abs
  to ensure that result is non-negative.

* test-suite/tests/numbers.test ("gcd"): New test, (gcd -2).
---
 NEWS                          |    1 +
 libguile/numbers.c            |    2 +-
 test-suite/tests/numbers.test |    5 +++++
 3 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/NEWS b/NEWS
index d5e4510..f4ca739 100644
--- a/NEWS
+++ b/NEWS
@@ -34,6 +34,7 @@ available: Guile is now always configured in "maintainer mode".
 ** Fix build issue on hppa2.0w-hp-hpux11.11 (`dirent64' and `readdir64_r')
 ** Fix misleading output from `(help rationalize)'
 ** Fix build failure on Debian hppa architecture (bad stack growth detection)
+** Fix `gcd' when called with a single, negative argument.
 
 
 Changes in 1.8.5 (since 1.8.4)
diff --git a/libguile/numbers.c b/libguile/numbers.c
index 7a4d619..52dfb73 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -1022,7 +1022,7 @@ SCM
 scm_gcd (SCM x, SCM y)
 {
   if (SCM_UNBNDP (y))
-    return SCM_UNBNDP (x) ? SCM_INUM0 : x;
+    return SCM_UNBNDP (x) ? SCM_INUM0 : scm_abs (x);
   
   if (SCM_I_INUMP (x))
     {
diff --git a/test-suite/tests/numbers.test b/test-suite/tests/numbers.test
index 2c004f5..2dbc917 100644
--- a/test-suite/tests/numbers.test
+++ b/test-suite/tests/numbers.test
@@ -1059,6 +1059,11 @@
   (expect-fail "documented?"
     (documented? gcd))
 
+  (with-test-prefix "(n)"
+
+    (pass-if "n = -2"
+      (eqv? 2 (gcd -2))))
+
   (with-test-prefix "(0 n)"
 
     (pass-if "n = 0"
-- 
1.5.6.5

Reply via email to