Fix for incorrect (gcd -2) => -2; should give 2.
[bpt/guile.git] / libguile / numbers.c
index 9c2a651..52dfb73 100644 (file)
 
  */
 
-/* tell glibc (2.3) to give prototype for C99 trunc(), csqrt(), etc */
-#define _GNU_SOURCE
-
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
@@ -163,20 +160,24 @@ xisnan (double x)
 }
 
 #if defined (GUILE_I)
+#if HAVE_COMPLEX_DOUBLE
+
 /* For an SCM object Z which is a complex number (ie. satisfies
    SCM_COMPLEXP), return its value as a C level "complex double". */
 #define SCM_COMPLEX_VALUE(z)                                    \
   (SCM_COMPLEX_REAL (z) + GUILE_I * SCM_COMPLEX_IMAG (z))
-#endif
+
+static inline SCM scm_from_complex_double (complex double z) SCM_UNUSED;
 
 /* Convert a C "complex double" to an SCM value. */
-#if HAVE_COMPLEX_DOUBLE
-static SCM
+static inline SCM
 scm_from_complex_double (complex double z)
 {
   return scm_c_make_rectangular (creal (z), cimag (z));
 }
+
 #endif /* HAVE_COMPLEX_DOUBLE */
+#endif /* GUILE_I */
 
 \f
 
@@ -1021,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))
     {
@@ -5336,13 +5337,13 @@ scm_c_make_rectangular (double re, double im)
 }
 
 SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
-            (SCM real, SCM imaginary),
-           "Return a complex number constructed of the given @var{real} and\n"
-           "@var{imaginary} parts.")
+            (SCM real_part, SCM imaginary_part),
+           "Return a complex number constructed of the given @var{real-part} "
+           "and @var{imaginary-part} parts.")
 #define FUNC_NAME s_scm_make_rectangular
 {
   struct dpair xy;
-  scm_two_doubles (real, imaginary, FUNC_NAME, &xy);
+  scm_two_doubles (real_part, imaginary_part, FUNC_NAME, &xy);
   return scm_c_make_rectangular (xy.x, xy.y);
 }
 #undef FUNC_NAME
@@ -5595,8 +5596,18 @@ SCM_DEFINE (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
 #undef FUNC_NAME
 
 SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0, 
-            (SCM x, SCM err),
-           "Return an exact number that is within @var{err} of @var{x}.")
+            (SCM x, SCM eps),
+           "Returns the @emph{simplest} rational number differing\n"
+           "from @var{x} by no more than @var{eps}.\n"
+           "\n"
+           "As required by @acronym{R5RS}, @code{rationalize} only returns an\n"
+           "exact result when both its arguments are exact.  Thus, you might need\n"
+           "to use @code{inexact->exact} on the arguments.\n"
+           "\n"
+           "@lisp\n"
+           "(rationalize (inexact->exact 1.2) 1/100)\n"
+           "@result{} 6/5\n"
+           "@end lisp")
 #define FUNC_NAME s_scm_rationalize
 {
   if (SCM_I_INUMP (x))
@@ -5628,7 +5639,7 @@ SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
         converges after less than a dozen iterations.
       */
 
-      err = scm_abs (err);
+      eps = scm_abs (eps);
       while (++i < 1000000)
        {
          a = scm_sum (scm_product (a1, tt), a2);    /* a = a1*tt + a2 */
@@ -5636,11 +5647,11 @@ SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
          if (scm_is_false (scm_zero_p (b)) &&         /* b != 0 */
              scm_is_false 
              (scm_gr_p (scm_abs (scm_difference (ex, scm_divide (a, b))),
-                        err)))                      /* abs(x-a/b) <= err */
+                        eps)))                      /* abs(x-a/b) <= eps */
            {
              SCM res = scm_sum (int_part, scm_divide (a, b));
              if (scm_is_false (scm_exact_p (x))
-                 || scm_is_false (scm_exact_p (err)))
+                 || scm_is_false (scm_exact_p (eps)))
                return scm_exact_to_inexact (res);
              else
                return res;