Remove dump facilities.
[bpt/guile.git] / libguile / numbers.c
index b64b8fd..2b2cedb 100644 (file)
@@ -52,7 +52,6 @@
 #include "libguile/root.h"
 #include "libguile/smob.h"
 #include "libguile/strings.h"
-#include "libguile/vectors.h"
 
 #include "libguile/validate.h"
 #include "libguile/numbers.h"
@@ -97,10 +96,15 @@ static SCM scm_divbigint (SCM x, long z, int sgn, int mode);
 
 \f
 
+static SCM abs_most_negative_fixnum;
+
+\f
+
 
 SCM_DEFINE (scm_exact_p, "exact?", 1, 0, 0, 
             (SCM x),
-           "Return #t if X is an exact number, #f otherwise.")
+           "Return @code{#t} if @var{x} is an exact number, @code{#f}\n"
+           "otherwise.")
 #define FUNC_NAME s_scm_exact_p
 {
   if (SCM_INUMP (x)) {
@@ -116,7 +120,8 @@ SCM_DEFINE (scm_exact_p, "exact?", 1, 0, 0,
 
 SCM_DEFINE (scm_odd_p, "odd?", 1, 0, 0, 
             (SCM n),
-           "Return #t if N is an odd number, #f otherwise.")
+           "Return @code{#t} if @var{n} is an odd number, @code{#f}\n"
+           "otherwise.")
 #define FUNC_NAME s_scm_odd_p
 {
   if (SCM_INUMP (n)) {
@@ -132,7 +137,8 @@ SCM_DEFINE (scm_odd_p, "odd?", 1, 0, 0,
 
 SCM_DEFINE (scm_even_p, "even?", 1, 0, 0, 
             (SCM n),
-           "Return #t if N is an even number, #f otherwise.")
+           "Return @code{#t} if @var{n} is an even number, @code{#f}\n"
+           "otherwise.")
 #define FUNC_NAME s_scm_even_p
 {
   if (SCM_INUMP (n)) {
@@ -147,7 +153,8 @@ SCM_DEFINE (scm_even_p, "even?", 1, 0, 0,
 
 
 SCM_GPROC (s_abs, "abs", 1, 0, 0, scm_abs, g_abs);
-
+/* "Return the absolute value of @var{x}."
+ */
 SCM
 scm_abs (SCM x)
 {
@@ -179,7 +186,8 @@ scm_abs (SCM x)
 
 
 SCM_GPROC (s_quotient, "quotient", 2, 0, 0, scm_quotient, g_quotient);
-
+/* "Return the quotient of the numbers @var{x} and @var{y}."
+ */
 SCM
 scm_quotient (SCM x, SCM y)
 {
@@ -202,7 +210,14 @@ scm_quotient (SCM x, SCM y)
        }
       }
     } else if (SCM_BIGP (y)) {
-      return SCM_INUM0;
+      if (SCM_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM
+         && scm_bigcomp (abs_most_negative_fixnum, y) == 0)
+       {
+         /* Special case:  x == fixnum-min && y == abs (fixnum-min) */
+         return SCM_MAKINUM (-1);
+       }
+      else
+       return SCM_MAKINUM (0);
     } else {
       SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG2, s_quotient);
     }
@@ -249,7 +264,12 @@ scm_quotient (SCM x, SCM y)
 
 
 SCM_GPROC (s_remainder, "remainder", 2, 0, 0, scm_remainder, g_remainder);
-
+/* "Return the remainder of the numbers @var{x} and @var{y}.\n"
+ * "@lisp\n"
+ * "(remainder 13 4) @result{} 1\n"
+ * "(remainder -13 4) @result{} -1\n"
+ * "@end lisp"
+ */
 SCM
 scm_remainder (SCM x, SCM y)
 {
@@ -263,7 +283,14 @@ scm_remainder (SCM x, SCM y)
        return SCM_MAKINUM (z);
       }
     } else if (SCM_BIGP (y)) {
-      return x;
+      if (SCM_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM
+         && scm_bigcomp (abs_most_negative_fixnum, y) == 0)
+       {
+         /* Special case:  x == fixnum-min && y == abs (fixnum-min) */
+         return SCM_MAKINUM (0);
+       }
+      else
+       return x;
     } else {
       SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG2, s_remainder);
     }
@@ -289,7 +316,12 @@ scm_remainder (SCM x, SCM y)
 
 
 SCM_GPROC (s_modulo, "modulo", 2, 0, 0, scm_modulo, g_modulo);
-
+/* "Return the modulo of the numbers @var{x} and @var{y}.\n"
+ * "@lisp\n"
+ * "(modulo 13 4) @result{} 1\n"
+ * "(modulo -13 4) @result{} 3\n"
+ * "@end lisp"
+ */
 SCM
 scm_modulo (SCM x, SCM y)
 {
@@ -332,7 +364,9 @@ scm_modulo (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_gcd, "gcd", scm_tc7_asubr, scm_gcd, g_gcd);
-
+/* "Return the greatest common divisor of all arguments.\n"
+ * "If called without arguments, 0 is returned."
+ */
 SCM
 scm_gcd (SCM x, SCM y)
 {
@@ -445,7 +479,9 @@ scm_gcd (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_lcm, "lcm", scm_tc7_asubr, scm_lcm, g_lcm);
-
+/* "Return the least common multiple of the arguments.\n"
+ * "If called without arguments, 1 is returned."
+ */
 SCM
 scm_lcm (SCM n1, SCM n2)
 {
@@ -655,12 +691,14 @@ SCM scm_big_and(SCM_BIGDIG *x, scm_sizet nx, int xsgn, SCM bigy, int zsgn)
       if (!num) return scm_normbig(z);
     }
   }
-  else if (xsgn) do {
-    num += x[i];
-    if (num < 0) {zds[i] &= num + SCM_BIGRAD; num = -1;}
-    else {zds[i] &= ~SCM_BIGLO(num); num = 0;}
-  } while (++i < nx);
-  else do zds[i] = zds[i] & x[i]; while (++i < nx);
+  else if (xsgn) {
+    unsigned long int carry = 1;
+    do {
+      unsigned long int mask = (SCM_BIGDIG) ~x[i] + carry;
+      zds[i] = zds[i] & (SCM_BIGDIG) mask;
+      carry = (mask >= SCM_BIGRAD) ? 1 : 0;
+    } while (++i < nx);
+  } else do zds[i] = zds[i] & x[i]; while (++i < nx);
   return scm_normbig(z);
 }
 
@@ -840,7 +878,7 @@ SCM_DEFINE1 (scm_logior, "logior", scm_tc7_asubr,
                              (nn1 < 0) ? SCM_BIGSIGNFLAG : 0, n2, SCM_BIGSIGNFLAG);
        }
 # else
-       BIGDIG zdigs [DIGSPERLONG];
+       SCM_BIGDIG zdigs [SCM_DIGSPERLONG];
        scm_longdigs (nn1, zdigs);
        if ((!(nn1 < 0)) && !SCM_BIGSIGN (n2)) {
          return scm_big_ior (zdigs, SCM_DIGSPERLONG, 
@@ -1062,8 +1100,7 @@ SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0,
            "   @result{} \"-10000001\"\n"
            "(number->string (lognot #b0) 2)\n"
            "   @result{} \"-1\"\n"
-           "@end lisp\n"
-           "")
+           "@end lisp\n")
 #define FUNC_NAME s_scm_lognot
 {
   return scm_difference (SCM_MAKINUM (-1L), n);
@@ -1112,20 +1149,21 @@ SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0,
 
 SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
             (SCM n, SCM cnt),
-           "The function ash performs an arithmetic shift left by CNT bits\n"
-           "(or shift right, if CNT is negative).  'Arithmetic' means, that\n"
-            "the function does not guarantee to keep the bit structure of N,\n"
-            "but rather guarantees that the result will always be rounded\n"
-           "towards minus infinity.  Therefore, the results of ash and a\n"
-           "corresponding bitwise shift will differ if N is negative.\n\n"
+           "The function ash performs an arithmetic shift left by @var{CNT}\n"
+           "bits (or shift right, if @var{cnt} is negative).\n"
+           "'Arithmetic' means, that the function does not guarantee to\n"
+           "keep the bit structure of @var{n}, but rather guarantees that\n"
+           "the result will always be rounded towards minus infinity.\n"
+           "Therefore, the results of ash and a corresponding bitwise\n"
+           "shift will differ if N is negative.\n\n"
            "Formally, the function returns an integer equivalent to\n"
-           "@code{(inexact->exact (floor (* N (expt 2 CNT))))}.@refill\n\n"
+           "@code{(inexact->exact (floor (* @var{n} (expt 2 @var{cnt}))))}.\n\n"
            "Example:\n"
            "@lisp\n"
            "(number->string (ash #b1 3) 2)\n"
-           "   @result{} \"1000\""
-           "(number->string (ash #b1010 -1) 2)"
-           "   @result{} \"101\""
+           "   @result{} \"1000\"\n"
+           "(number->string (ash #b1010 -1) 2)\n"
+           "   @result{} \"101\"\n"
            "@end lisp")
 #define FUNC_NAME s_scm_ash
 {
@@ -1182,19 +1220,50 @@ SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
            "@end lisp")
 #define FUNC_NAME s_scm_bit_extract
 {
-  int istart, iend;
+  unsigned long int istart, iend;
   SCM_VALIDATE_INUM_MIN_COPY (2,start,0,istart);
   SCM_VALIDATE_INUM_MIN_COPY (3, end, 0, iend);
   SCM_ASSERT_RANGE (3, end, (iend >= istart));
 
   if (SCM_INUMP (n)) {
-    return SCM_MAKINUM ((SCM_INUM (n) >> istart) & ((1L << (iend - istart)) - 1));
+    long int in = SCM_INUM (n);
+    unsigned long int bits = iend - istart;
+
+    if (in < 0 && bits >= SCM_FIXNUM_BIT)
+      {
+       /* Since we emulate two's complement encoded numbers, this special
+        * case requires us to produce a result that has more bits than can be
+        * stored in a fixnum.  Thus, we fall back to the more general
+        * algorithm that is used for bignums.  
+        */
+       goto generalcase;
+      }
+
+    if (istart < SCM_FIXNUM_BIT)
+      {
+       in = in >> istart;
+       if (bits < SCM_FIXNUM_BIT)
+         return SCM_MAKINUM (in & ((1L << bits) - 1));
+       else /* we know: in >= 0 */
+         return SCM_MAKINUM (in);
+      }
+    else if (in < 0)
+      {
+       return SCM_MAKINUM (-1L & ((1L << bits) - 1));
+      }
+    else
+      {
+       return SCM_MAKINUM (0);
+      }
   } else if (SCM_BIGP (n)) {
-    SCM num1 = SCM_MAKINUM (1L);
-    SCM num2 = SCM_MAKINUM (2L);
-    SCM bits = SCM_MAKINUM (iend - istart);
-    SCM mask  = scm_difference (scm_integer_expt (num2, bits), num1);
-    return scm_logand (mask, scm_ash (n, SCM_MAKINUM (-istart)));
+  generalcase:
+    {
+      SCM num1 = SCM_MAKINUM (1L);
+      SCM num2 = SCM_MAKINUM (2L);
+      SCM bits = SCM_MAKINUM (iend - istart);
+      SCM mask  = scm_difference (scm_integer_expt (num2, bits), num1);
+      return scm_logand (mask, scm_ash (n, SCM_MAKINUM (-istart)));
+    }
   } else {
     SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
   }
@@ -1324,8 +1393,7 @@ scm_mkbig (scm_sizet nlen, int sign)
   
   SCM_NEWCELL (v);
   SCM_DEFER_INTS;
-  SCM_SETCHARS (v, scm_must_malloc ((long) (nlen * sizeof (SCM_BIGDIG)),
-                                   s_bignum));
+  SCM_SET_BIGNUM_BASE (v, scm_must_malloc (nlen * sizeof (SCM_BIGDIG), s_bignum));
   SCM_SETNUMDIGS (v, nlen, sign);
   SCM_ALLOW_INTS;
   return v;
@@ -1363,11 +1431,11 @@ scm_adjbig (SCM b, scm_sizet nlen)
   {
     SCM_BIGDIG *digits
       = ((SCM_BIGDIG *)
-        scm_must_realloc ((char *) SCM_CHARS (b),
+        scm_must_realloc ((char *) SCM_BDIGITS (b),
                           (long) (SCM_NUMDIGS (b) * sizeof (SCM_BIGDIG)),
                           (long) (nsiz * sizeof (SCM_BIGDIG)), s_bignum));
 
-    SCM_SETCHARS (b, digits);
+    SCM_SET_BIGNUM_BASE (b, digits);
     SCM_SETNUMDIGS (b, nsiz, SCM_BIGSIGN (b));
   }
   SCM_ALLOW_INTS;
@@ -2176,16 +2244,14 @@ big2str (SCM b, unsigned int radix)
     : (SCM_BITSPERDIG * i) + 2;
   scm_sizet k = 0;
   scm_sizet radct = 0;
-  scm_sizet ch;                        /* jeh */
   SCM_BIGDIG radpow = 1, radmod = 0;
   SCM ss = scm_makstr ((long) j, 0);
-  char *s = SCM_CHARS (ss), c;
+  char *s = SCM_STRING_CHARS (ss), c;
   while ((long) radpow * radix < SCM_BIGRAD)
     {
       radpow *= radix;
       radct++;
     }
-  s[0] = SCM_BIGSIGN (b) ? '-' : '+';
   while ((i || radmod) && j)
     {
       if (k == 0)
@@ -2200,13 +2266,15 @@ big2str (SCM b, unsigned int radix)
       k--;
       s[--j] = c < 10 ? c + '0' : c + 'a' - 10;
     }
-  ch = s[0] == '-' ? 1 : 0;    /* jeh */
-  if (ch < j)
-    {                          /* jeh */
-      for (i = j; j < SCM_LENGTH (ss); j++)
-       s[ch + j - i] = s[j];   /* jeh */
-      scm_vector_set_length_x (ss, /* jeh */
-                              SCM_MAKINUM (ch + SCM_LENGTH (ss) - i));
+
+  if (SCM_BIGSIGN (b))
+    s[--j] = '-';
+
+  if (j > 0)
+    {
+      /* The pre-reserved string length was too large. */
+      unsigned long int length = SCM_STRING_LENGTH (ss);
+      ss = scm_substring (ss, SCM_MAKINUM (j), SCM_MAKINUM (length));
     }
 
   return scm_return_first (ss, t);
@@ -2217,8 +2285,8 @@ big2str (SCM b, unsigned int radix)
 SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
             (SCM n, SCM radix),
            "Return a string holding the external representation of the\n"
-           "number N in the given RADIX.  If N is inexact, a radix of 10\n"
-           "will be used.")
+           "number @var{n} in the given @var{radix}.  If @var{n} is\n"
+           "inexact, a radix of 10 will be used.")
 #define FUNC_NAME s_scm_number_to_string
 {
   int base;
@@ -2271,7 +2339,7 @@ scm_bigprint (SCM exp, SCM port, scm_print_state *pstate)
 {
 #ifdef SCM_BIGDIG
   exp = big2str (exp, (unsigned int) 10);
-  scm_lfwrite (SCM_CHARS (exp), (scm_sizet) SCM_LENGTH (exp), port);
+  scm_lfwrite (SCM_STRING_CHARS (exp), (scm_sizet) SCM_STRING_LENGTH (exp), port);
 #else
   scm_ipruk ("bignum", exp, port);
 #endif
@@ -2641,7 +2709,7 @@ scm_istr2flo (char *str, long len, long radix)
              case DIGITS:
                expon = expon * 10 + c - '0';
                if (expon > SCM_MAXEXP)
-                 return SCM_BOOL_F;    /* exponent too large */
+                 scm_out_of_range ("string->number", SCM_MAKINUM (expon));
                break;
              default:
                goto out4;
@@ -2797,20 +2865,21 @@ scm_istring2number (char *str, long len, long radix)
 SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
             (SCM string, SCM radix),
            "Returns a number of the maximally precise representation\n"
-           "expressed by the given STRING. RADIX must be an exact integer,\n"
-           "either 2, 8, 10, or 16. If supplied, RADIX is a default radix\n"
-           "that may be overridden by an explicit radix prefix in STRING\n"
-           "(e.g. \"#o177\"). If RADIX is not supplied, then the default\n"
-           "radix is 10. If string is not a syntactically valid notation\n"
-           "for a number, then `string->number' returns #f.  (r5rs)") 
+           "expressed by the given @var{string}. @var{radix} must be an\n"
+           "exact integer, either 2, 8, 10, or 16. If supplied, @var{RADIX}\n"
+           "is a default radix that may be overridden by an explicit\n"
+           "radix prefix in @var{string} (e.g. \"#o177\"). If @var{radix}\n"
+           "is not supplied, then the default radix is 10. If string is\n"
+           "not a syntactically valid notation for a number, then\n"
+           "@code{string->number} returns @code{#f}.  (r5rs)") 
 #define FUNC_NAME s_scm_string_to_number
 {
   SCM answer;
   int base;
-  SCM_VALIDATE_ROSTRING (1,string);
+  SCM_VALIDATE_STRING (1, string);
   SCM_VALIDATE_INUM_MIN_DEF_COPY (2,radix,2,10,base);
-  answer = scm_istring2number (SCM_ROCHARS (string),
-                              SCM_ROLENGTH (string),
+  answer = scm_istring2number (SCM_STRING_CHARS (string),
+                              SCM_STRING_LENGTH (string),
                                base);
   return scm_return_first (answer, string);
 }
@@ -2870,13 +2939,18 @@ scm_complex_equalp (SCM x, SCM y)
 
 
 SCM_REGISTER_PROC (s_number_p, "number?", 1, 0, 0, scm_number_p);
-
+/* "Return @code{#t} if @var{x} is a number, @code{#f}\n"
+ * "else.  Note that the sets of complex, real, rational and\n"
+ * "integer values form subsets of the set of numbers, i. e. the\n"
+ * "predicate will be fulfilled for any number."
+ */
 SCM_DEFINE (scm_number_p, "complex?", 1, 0, 0, 
             (SCM x),
-           "Return #t if X is a complex number, #f else.  Note that the\n"
-           "sets of real, rational and integer values form subsets of the\n"
-           "set of complex numbers, i. e. the predicate will also be\n"
-           "fulfilled if X is a real, rational or integer number.")
+           "Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
+           "else.  Note that the sets of real, rational and integer\n"
+           "values form subsets of the set of complex numbers, i. e. the\n"
+           "predicate will also be fulfilled if @var{x} is a real,\n"
+           "rational or integer number.")
 #define FUNC_NAME s_scm_number_p
 {
   return SCM_BOOL (SCM_NUMBERP (x));
@@ -2885,13 +2959,19 @@ SCM_DEFINE (scm_number_p, "complex?", 1, 0, 0,
 
 
 SCM_REGISTER_PROC (s_real_p, "real?", 1, 0, 0, scm_real_p);
-
+/* "Return @code{#t} if @var{x} is a real number, @code{#f} else.\n"
+ * "Note that the sets of integer and rational values form a subset\n"
+ * "of the set of real numbers, i. e. the predicate will also\n"
+ * "be fulfilled if @var{x} is an integer or a rational number."
+ */
 SCM_DEFINE (scm_real_p, "rational?", 1, 0, 0, 
             (SCM x),
-           "Return #t if X is a rational number, #f else.  Note that the\n"
-           "set of integer values forms a subset of the set of rational\n"
-           "numbers, i. e. the predicate will also be fulfilled if X is an\n"
-           "integer number.")
+           "Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
+           "else.  Note that the set of integer values forms a subset of\n"
+           "the set of rational numbers, i. e. the predicate will also be\n"
+           "fulfilled if @var{x} is an integer number.  Real numbers\n"
+           "will also satisfy this predicate, because of their limited\n"
+           "precision.")
 #define FUNC_NAME s_scm_real_p
 {
   if (SCM_INUMP (x)) {
@@ -2911,7 +2991,8 @@ SCM_DEFINE (scm_real_p, "rational?", 1, 0, 0,
 
 SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0, 
             (SCM x),
-           "Return #t if X is an integer number, #f else.")
+           "Return @code{#t} if @var{x} is an integer number, @code{#f}\n"
+           "else.")
 #define FUNC_NAME s_scm_integer_p
 {
   double r;
@@ -2935,7 +3016,8 @@ SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
 
 SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0, 
             (SCM x),
-           "Return #t if X is an inexact number, #f else.")
+           "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n"
+           "else.")
 #define FUNC_NAME s_scm_inexact_p
 {
   return SCM_BOOL (SCM_INEXACTP (x));
@@ -2944,7 +3026,7 @@ SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0,
 
 
 SCM_GPROC1 (s_eq_p, "=", scm_tc7_rpsubr, scm_num_eq_p, g_eq_p);
-
+/* "Return @code{#t} if all parameters are numerically equal."  */
 SCM
 scm_num_eq_p (SCM x, SCM y)
 {
@@ -3012,7 +3094,9 @@ scm_num_eq_p (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_less_p, "<", scm_tc7_rpsubr, scm_less_p, g_less_p);
-
+/* "Return @code{#t} if the list of parameters is monotonically\n"
+ * "increasing."
+ */
 SCM
 scm_less_p (SCM x, SCM y)
 {
@@ -3055,8 +3139,8 @@ scm_less_p (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_scm_gr_p, ">", scm_tc7_rpsubr, scm_gr_p, g_gr_p);
-/* "Return #t if the list of parameters is monotonically\n"
- *  "increasing."
+/* "Return @code{#t} if the list of parameters is monotonically\n"
+ * "decreasing."
  */
 #define FUNC_NAME s_scm_gr_p
 SCM
@@ -3073,7 +3157,7 @@ scm_gr_p (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_scm_leq_p, "<=", scm_tc7_rpsubr, scm_leq_p, g_leq_p);
-/* "Return #t if the list of parameters is monotonically\n"
+/* "Return @code{#t} if the list of parameters is monotonically\n"
  * "non-decreasing."
  */
 #define FUNC_NAME s_scm_leq_p
@@ -3091,7 +3175,7 @@ scm_leq_p (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_scm_geq_p, ">=", scm_tc7_rpsubr, scm_geq_p, g_geq_p);
-/* "Return #t if the list of parameters is monotonically\n"
+/* "Return @code{#t} if the list of parameters is monotonically\n"
  * "non-increasing."
  */
 #define FUNC_NAME s_scm_geq_p
@@ -3109,7 +3193,9 @@ scm_geq_p (SCM x, SCM y)
 
 
 SCM_GPROC (s_zero_p, "zero?", 1, 0, 0, scm_zero_p, g_zero_p);
-
+/* "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
+ * "zero."
+ */
 SCM
 scm_zero_p (SCM z)
 {
@@ -3129,7 +3215,9 @@ scm_zero_p (SCM z)
 
 
 SCM_GPROC (s_positive_p, "positive?", 1, 0, 0, scm_positive_p, g_positive_p);
-
+/* "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
+ * "zero."
+ */
 SCM
 scm_positive_p (SCM x)
 {
@@ -3146,7 +3234,9 @@ scm_positive_p (SCM x)
 
 
 SCM_GPROC (s_negative_p, "negative?", 1, 0, 0, scm_negative_p, g_negative_p);
-
+/* "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
+ * "zero."
+ */
 SCM
 scm_negative_p (SCM x)
 {
@@ -3163,7 +3253,8 @@ scm_negative_p (SCM x)
 
 
 SCM_GPROC1 (s_max, "max", scm_tc7_asubr, scm_max, g_max);
-
+/* "Return the maximum of all parameter values."
+ */
 SCM
 scm_max (SCM x, SCM y)
 {
@@ -3220,7 +3311,8 @@ scm_max (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_min, "min", scm_tc7_asubr, scm_min, g_min);
-
+/* "Return the minium of all parameter values."
+ */
 SCM
 scm_min (SCM x, SCM y)
 {
@@ -3277,7 +3369,9 @@ scm_min (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_sum, "+", scm_tc7_asubr, scm_sum, g_sum);
-
+/* "Return the sum of all parameter values.  Return 0 if called without\n"
+ * "any parameters." 
+ */
 SCM
 scm_sum (SCM x, SCM y)
 {
@@ -3382,7 +3476,10 @@ scm_sum (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_difference, "-", scm_tc7_asubr, scm_difference, g_difference);
-
+/* "If called without arguments, 0 is returned. Otherwise the sum of\n"
+ * "all but the first argument are subtracted from the first\n"
+ * "argument."
+ */
 SCM
 scm_difference (SCM x, SCM y)
 {
@@ -3508,7 +3605,9 @@ scm_difference (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_product, "*", scm_tc7_asubr, scm_product, g_product);
-
+/* "Return the product of all arguments.  If called without arguments,\n"
+ * "1 is returned."
+ */
 SCM
 scm_product (SCM x, SCM y)
 {
@@ -3655,7 +3754,8 @@ scm_num2dbl (SCM a, const char *why)
 
 
 SCM_GPROC1 (s_divide, "/", scm_tc7_asubr, scm_divide, g_divide);
-
+/* "Divide the first argument by the product of the remaining arguments."
+ */
 SCM
 scm_divide (SCM x, SCM y)
 {
@@ -3811,7 +3911,8 @@ scm_divide (SCM x, SCM y)
 
 
 SCM_GPROC1 (s_asinh, "$asinh", scm_tc7_cxr, (SCM (*)()) scm_asinh, g_asinh);
-
+/* "Return the inverse hyperbolic sine of @var{x}."
+ */
 double
 scm_asinh (double x)
 {
@@ -3822,7 +3923,8 @@ scm_asinh (double x)
 
 
 SCM_GPROC1 (s_acosh, "$acosh", scm_tc7_cxr, (SCM (*)()) scm_acosh, g_acosh);
-
+/* "Return the inverse hyperbolic cosine of @var{x}."
+ */
 double
 scm_acosh (double x)
 {
@@ -3833,7 +3935,8 @@ scm_acosh (double x)
 
 
 SCM_GPROC1 (s_atanh, "$atanh", scm_tc7_cxr, (SCM (*)()) scm_atanh, g_atanh);
-
+/* "Return the inverse hyperbolic tangent of @var{x}."
+ */
 double
 scm_atanh (double x)
 {
@@ -3844,7 +3947,8 @@ scm_atanh (double x)
 
 
 SCM_GPROC1 (s_truncate, "truncate", scm_tc7_cxr, (SCM (*)()) scm_truncate, g_truncate);
-
+/* "Round the inexact number @var{x} towards zero."
+ */
 double
 scm_truncate (double x)
 {
@@ -3856,7 +3960,9 @@ scm_truncate (double x)
 
 
 SCM_GPROC1 (s_round, "round", scm_tc7_cxr, (SCM (*)()) scm_round, g_round);
-
+/* "Round the inexact number @var{x}. If @var{x} is halfway between two\n"
+ * "numbers, round towards even."
+ */
 double
 scm_round (double x)
 {
@@ -3870,7 +3976,8 @@ scm_round (double x)
 
 
 SCM_GPROC1 (s_exact_to_inexact, "exact->inexact", scm_tc7_cxr, (SCM (*)()) scm_exact_to_inexact, g_exact_to_inexact);
-
+/* Convert the number @var{x} to its inexact representation.\n" 
+ */
 double
 scm_exact_to_inexact (double z)
 {
@@ -3879,75 +3986,110 @@ scm_exact_to_inexact (double z)
 
 
 SCM_GPROC1 (s_i_floor, "floor", scm_tc7_cxr, (SCM (*)()) floor, g_i_floor);
+/* "Round the number @var{x} towards minus infinity."
+ */
 SCM_GPROC1 (s_i_ceil, "ceiling", scm_tc7_cxr, (SCM (*)()) ceil, g_i_ceil);
+/* "Round the number @var{x} towards infinity."
+ */
 SCM_GPROC1 (s_i_sqrt, "$sqrt", scm_tc7_cxr, (SCM (*)()) sqrt, g_i_sqrt);
+/* "Return the square root of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_abs, "$abs", scm_tc7_cxr, (SCM (*)()) fabs, g_i_abs);
+/* "Return the absolute value of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_exp, "$exp", scm_tc7_cxr, (SCM (*)()) exp, g_i_exp);
+/* "Return the @var{x}th power of e."
+ */
 SCM_GPROC1 (s_i_log, "$log", scm_tc7_cxr, (SCM (*)()) log, g_i_log);
+/* "Return the natural logarithm of the real number@var{x}."
+ */
 SCM_GPROC1 (s_i_sin, "$sin", scm_tc7_cxr, (SCM (*)()) sin, g_i_sin);
+/* "Return the sine of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_cos, "$cos", scm_tc7_cxr, (SCM (*)()) cos, g_i_cos);
+/* "Return the cosine of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_tan, "$tan", scm_tc7_cxr, (SCM (*)()) tan, g_i_tan);
+/* "Return the tangent of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_asin, "$asin", scm_tc7_cxr, (SCM (*)()) asin, g_i_asin);
+/* "Return the arc sine of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_acos, "$acos", scm_tc7_cxr, (SCM (*)()) acos, g_i_acos);
+/* "Return the arc cosine of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_atan, "$atan", scm_tc7_cxr, (SCM (*)()) atan, g_i_atan);
+/* "Return the arc tangent of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_sinh, "$sinh", scm_tc7_cxr, (SCM (*)()) sinh, g_i_sinh);
+/* "Return the hyperbolic sine of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_cosh, "$cosh", scm_tc7_cxr, (SCM (*)()) cosh, g_i_cosh);
+/* "Return the hyperbolic cosine of the real number @var{x}."
+ */
 SCM_GPROC1 (s_i_tanh, "$tanh", scm_tc7_cxr, (SCM (*)()) tanh, g_i_tanh);
+/* "Return the hyperbolic tangent of the real number @var{x}."
+ */
 
 struct dpair
 {
   double x, y;
 };
 
-static void scm_two_doubles (SCM z1,
-                            SCM z2,
+static void scm_two_doubles (SCM x,
+                            SCM y,
                             const char *sstring,
                             struct dpair * xy);
 
 static void
-scm_two_doubles (SCM z1, SCM z2, const char *sstring, struct dpair *xy)
+scm_two_doubles (SCM x, SCM y, const char *sstring, struct dpair *xy)
 {
-  if (SCM_INUMP (z1)) {
-    xy->x = SCM_INUM (z1);
-  } else if (SCM_BIGP (z1)) {
-    xy->x = scm_big2dbl (z1);
-  } else if (SCM_REALP (z1)) {
-    xy->x = SCM_REAL_VALUE (z1);
+  if (SCM_INUMP (x)) {
+    xy->x = SCM_INUM (x);
+  } else if (SCM_BIGP (x)) {
+    xy->x = scm_big2dbl (x);
+  } else if (SCM_REALP (x)) {
+    xy->x = SCM_REAL_VALUE (x);
   } else {
-    scm_wrong_type_arg (sstring, SCM_ARG1, z1);
+    scm_wrong_type_arg (sstring, SCM_ARG1, x);
   }
 
-  if (SCM_INUMP (z2)) {
-    xy->y = SCM_INUM (z2);
-  } else if (SCM_BIGP (z2)) {
-    xy->y = scm_big2dbl (z2);
-  } else if (SCM_REALP (z2)) {
-    xy->y = SCM_REAL_VALUE (z2);
+  if (SCM_INUMP (y)) {
+    xy->y = SCM_INUM (y);
+  } else if (SCM_BIGP (y)) {
+    xy->y = scm_big2dbl (y);
+  } else if (SCM_REALP (y)) {
+    xy->y = SCM_REAL_VALUE (y);
   } else {
-    scm_wrong_type_arg (sstring, SCM_ARG2, z2);
+    scm_wrong_type_arg (sstring, SCM_ARG2, y);
   }
 }
 
 
 SCM_DEFINE (scm_sys_expt, "$expt", 2, 0, 0,
-            (SCM z1, SCM z2),
-           "") 
+            (SCM x, SCM y),
+           "Return @var{x} raised to the power of @var{y}. This\n"
+           "procedure does not accept complex arguments.") 
 #define FUNC_NAME s_scm_sys_expt
 {
   struct dpair xy;
-  scm_two_doubles (z1, z2, FUNC_NAME, &xy);
+  scm_two_doubles (x, y, FUNC_NAME, &xy);
   return scm_make_real (pow (xy.x, xy.y));
 }
 #undef FUNC_NAME
 
 
 SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0,
-            (SCM z1, SCM z2),
-           "")
+            (SCM x, SCM y),
+           "Return the arc tangent of the two arguments @var{x} and\n"
+           "@var{y}. This is similar to calculating the arc tangent of\n"
+           "@var{x} / @var{y}, except that the signs of both arguments\n"
+           "are used to determine the quadrant of the result. This\n"
+           "procedure does not accept complex arguments.")
 #define FUNC_NAME s_scm_sys_atan2
 {
   struct dpair xy;
-  scm_two_doubles (z1, z2, FUNC_NAME, &xy);
+  scm_two_doubles (x, y, FUNC_NAME, &xy);
   return scm_make_real (atan2 (xy.x, xy.y));
 }
 #undef FUNC_NAME
@@ -3955,8 +4097,8 @@ SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0,
 
 SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
             (SCM real, SCM imaginary),
-           "Return a complex number constructed of the given REAL and\n"
-           "IMAGINARY parts.")
+           "Return a complex number constructed of the given @var{real} and\n"
+           "@var{imaginary} parts.")
 #define FUNC_NAME s_scm_make_rectangular
 {
   struct dpair xy;
@@ -3968,19 +4110,20 @@ SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
 
 
 SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
-            (SCM z1, SCM z2),
-           "Return the complex number Z1 * e^(i * Z2).")
+            (SCM x, SCM y),
+           "Return the complex number @var{x} * e^(i * @var{y}).")
 #define FUNC_NAME s_scm_make_polar
 {
   struct dpair xy;
-  scm_two_doubles (z1, z2, FUNC_NAME, &xy);
+  scm_two_doubles (x, y, FUNC_NAME, &xy);
   return scm_make_complex (xy.x * cos (xy.y), xy.x * sin (xy.y));
 }
 #undef FUNC_NAME
 
 
 SCM_GPROC (s_real_part, "real-part", 1, 0, 0, scm_real_part, g_real_part);
-
+/* "Return the real part of the number @var{z}."
+ */
 SCM
 scm_real_part (SCM z)
 {
@@ -3999,7 +4142,8 @@ scm_real_part (SCM z)
 
 
 SCM_GPROC (s_imag_part, "imag-part", 1, 0, 0, scm_imag_part, g_imag_part);
-
+/* "Return the imaginary part of the number @var{z}."
+ */
 SCM
 scm_imag_part (SCM z)
 {
@@ -4018,7 +4162,9 @@ scm_imag_part (SCM z)
 
 
 SCM_GPROC (s_magnitude, "magnitude", 1, 0, 0, scm_magnitude, g_magnitude);
-
+/* "Return the magnitude of the number @var{z}. This is the same as\n"
+ * "@code{abs} for real arguments, but also allows complex numbers."
+ */
 SCM
 scm_magnitude (SCM z)
 {
@@ -4054,7 +4200,8 @@ scm_magnitude (SCM z)
 
 
 SCM_GPROC (s_angle, "angle", 1, 0, 0, scm_angle, g_angle);
-
+/* "Return the angle of the complex number @var{z}."
+ */
 SCM
 scm_angle (SCM z)
 {
@@ -4082,7 +4229,7 @@ scm_angle (SCM z)
 
 SCM_DEFINE (scm_inexact_to_exact, "inexact->exact", 1, 0, 0, 
             (SCM z),
-           "Returns an exact number that is numerically closest to Z.")
+           "Returns an exact number that is numerically closest to @var{z}.")
 #define FUNC_NAME s_scm_inexact_to_exact
 {
   if (SCM_INUMP (z)) {
@@ -4355,6 +4502,16 @@ scm_num2ulong (SCM num, char *pos, const char *s_caller)
 void
 scm_init_numbers ()
 {
+  abs_most_negative_fixnum = scm_long2big (- SCM_MOST_NEGATIVE_FIXNUM);
+  scm_permanent_object (abs_most_negative_fixnum);
+
+  /* It may be possible to tune the performance of some algorithms by using
+   * the following constants to avoid the creation of bignums.  Please, before
+   * using these values, remember the two rules of program optimization:
+   * 1st Rule:  Don't do it.  2nd Rule (experts only):  Don't do it yet. */
+  scm_sysintern ("most-positive-fixnum", SCM_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
+  scm_sysintern ("most-negative-fixnum", SCM_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
+
   scm_add_feature ("complex");
   scm_add_feature ("inexact");
   scm_flo0 = scm_make_real (0.0);
@@ -4375,7 +4532,9 @@ scm_init_numbers ()
     scm_dblprec = scm_dblprec - 1;
   }
 #endif /* DBL_DIG */
+#ifndef SCM_MAGIC_SNARFER
 #include "libguile/numbers.x"
+#endif
 }
 
 /*