unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* (gcd -2) -> -2
@ 2008-08-11 18:15 Bill Schottstaedt
  2008-08-12  9:14 ` Neil Jerram
  0 siblings, 1 reply; 7+ messages in thread
From: Bill Schottstaedt @ 2008-08-11 18:15 UTC (permalink / raw)
  To: bug-guile

guile> (gcd -2)
-2

(or any negative single argument).






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (gcd -2) -> -2
  2008-08-11 18:15 (gcd -2) -> -2 Bill Schottstaedt
@ 2008-08-12  9:14 ` Neil Jerram
  2008-08-12 11:27   ` Bill Schottstaedt
  0 siblings, 1 reply; 7+ messages in thread
From: Neil Jerram @ 2008-08-12  9:14 UTC (permalink / raw)
  To: Bill Schottstaedt; +Cc: bug-guile

2008/8/11 Bill Schottstaedt <bil@ccrma.stanford.edu>:
> guile> (gcd -2)
> -2
>
> (or any negative single argument).

Is that wrong?

     Neil




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (gcd -2) -> -2
  2008-08-12  9:14 ` Neil Jerram
@ 2008-08-12 11:27   ` Bill Schottstaedt
  2008-08-12 13:56     ` Neil Jerram
  0 siblings, 1 reply; 7+ messages in thread
From: Bill Schottstaedt @ 2008-08-12 11:27 UTC (permalink / raw)
  To: Neil Jerram; +Cc: bug-guile

gcd is supposed to ignore factors of -1.

"<" is restricted to reals -- a complex arg should be an error.







^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (gcd -2) -> -2
  2008-08-12 11:27   ` Bill Schottstaedt
@ 2008-08-12 13:56     ` Neil Jerram
  2008-08-12 14:28       ` Mikael Djurfeldt
  0 siblings, 1 reply; 7+ messages in thread
From: Neil Jerram @ 2008-08-12 13:56 UTC (permalink / raw)
  To: Bill Schottstaedt; +Cc: bug-guile

2008/8/12 Bill Schottstaedt <bil@ccrma.stanford.edu>:
> 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.)

> "<" is restricted to reals -- a complex arg should be an error.

Again, is that specified?

Mathematically, I can't help wondering about Lim(x + iy) as y -> 0.
Or, in Scheme terms, about inexact numbers.  Does inexactness in
Scheme pertain only to the real dimension?

(Sorry that my knowledge of how these matters are specified is so poor!)

    Neil




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (gcd -2) -> -2
  2008-08-12 13:56     ` Neil Jerram
@ 2008-08-12 14:28       ` Mikael Djurfeldt
  2008-09-17 20:53         ` [PATCH] " Neil Jerram
  0 siblings, 1 reply; 7+ messages in thread
From: Mikael Djurfeldt @ 2008-08-12 14:28 UTC (permalink / raw)
  To: Neil Jerram; +Cc: Bill Schottstaedt, bug-guile

2008/8/12 Neil Jerram <neiljerram@googlemail.com>:
> 2008/8/12 Bill Schottstaedt <bil@ccrma.stanford.edu>:
>> 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.

     (gcd 32 -36)                           ==>  4
     (gcd)                                  ==>  0
     (lcm 32 -36)                           ==>  288
     (lcm 32.0 -36)                         ==>  288.0  ; inexact
     (lcm)                                  ==>  1

>> "<" is restricted to reals -- a complex arg should be an error.
>
> Again, is that specified?
>
> Mathematically, I can't help wondering about Lim(x + iy) as y -> 0.
> Or, in Scheme terms, about inexact numbers.  Does inexactness in
> Scheme pertain only to the real dimension?

Mathematically, < doesn't have a meaning if its arguments complex,
i.e. if y above isn't exactly zero.  In Scheme terms, I guess one has
to consider a number real if the imaginary part is 0.0 even though it
is an inexact real number...




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] Re: (gcd -2) -> -2
  2008-08-12 14:28       ` Mikael Djurfeldt
@ 2008-09-17 20:53         ` Neil Jerram
  2008-09-18  8:05           ` Ludovic Courtès
  0 siblings, 1 reply; 7+ messages in thread
From: Neil Jerram @ 2008-09-17 20:53 UTC (permalink / raw)
  To: Bill Schottstaedt; +Cc: bug-guile

[-- Attachment #1: Type: text/plain, Size: 632 bytes --]

2008/8/12 Mikael Djurfeldt <mikael@djurfeldt.com>:
> 2008/8/12 Neil Jerram <neiljerram@googlemail.com>:
>> 2008/8/12 Bill Schottstaedt <bil@ccrma.stanford.edu>:
>>> 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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-for-incorrect-gcd-2-2-should-give-2.patch --]
[-- Type: text/x-patch; name=0001-Fix-for-incorrect-gcd-2-2-should-give-2.patch, Size: 1812 bytes --]

From 420a9ff5588852795bb3bbb99f878f0471e8fe58 Mon Sep 17 00:00:00 2001
From: Neil Jerram <neil@ossau.uklinux.net>
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.
 
 \f
 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] Re: (gcd -2) -> -2
  2008-09-17 20:53         ` [PATCH] " Neil Jerram
@ 2008-09-18  8:05           ` Ludovic Courtès
  0 siblings, 0 replies; 7+ messages in thread
From: Ludovic Courtès @ 2008-09-18  8:05 UTC (permalink / raw)
  To: bug-guile

Hi,

"Neil Jerram" <neiljerram@googlemail.com> writes:

> Patch is attached, for review.

Looks good, thanks!

Ludo'.





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-09-18  8:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-11 18:15 (gcd -2) -> -2 Bill Schottstaedt
2008-08-12  9:14 ` Neil Jerram
2008-08-12 11:27   ` Bill Schottstaedt
2008-08-12 13:56     ` Neil Jerram
2008-08-12 14:28       ` Mikael Djurfeldt
2008-09-17 20:53         ` [PATCH] " Neil Jerram
2008-09-18  8:05           ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).