Remove dump facilities.
[bpt/guile.git] / libguile / numbers.c
index 5fa35f8..2b2cedb 100644 (file)
 #include <stdio.h>
 #include <math.h>
 #include "libguile/_scm.h"
-#include "libguile/unif.h"
 #include "libguile/feature.h"
 #include "libguile/ports.h"
 #include "libguile/root.h"
 #include "libguile/smob.h"
 #include "libguile/strings.h"
-#include "libguile/vectors.h"
 
 #include "libguile/validate.h"
 #include "libguile/numbers.h"
@@ -71,13 +69,26 @@ static SCM scm_divbigint (SCM x, long z, int sgn, int mode);
 #define SCM_SWAP(x,y) do { SCM __t = x; x = y; y = __t; } while (0)
 
 
+#if (SCM_DEBUG_DEPRECATED == 1)  /* not defined in header yet? */
+
+/* SCM_FLOBUFLEN is the maximum number of characters neccessary for the
+ * printed or scm_string representation of an inexact number.
+ */
+#define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
+
+#endif  /* SCM_DEBUG_DEPRECATED == 1 */
+
+
 /* IS_INF tests its floating point number for infiniteness
+   Dirk:FIXME:: This test does not work if x == 0
  */
 #ifndef IS_INF
-#define IS_INF(x) ((x) == (x) + 1)
+#define IS_INF(x) ((x) == (x) / 2)
 #endif
 
+
 /* Return true if X is not infinite and is not a NaN
+   Dirk:FIXME:: Since IS_INF is broken, this test does not work if x == 0
  */
 #ifndef isfinite
 #define isfinite(x) (!IS_INF (x) && (x) == (x))
@@ -85,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)) {
@@ -104,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)) {
@@ -120,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)) {
@@ -135,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)
 {
@@ -158,6 +177,8 @@ scm_abs (SCM x)
     } else {
       return scm_copybig (x, 0);
     }
+  } else if (SCM_REALP (x)) {
+    return scm_make_real (fabs (SCM_REAL_VALUE (x)));
   } else {
     SCM_WTA_DISPATCH_1 (g_abs, x, 1, s_abs);
   }
@@ -165,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)
 {
@@ -188,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);
     }
@@ -235,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)
 {
@@ -245,15 +279,18 @@ scm_remainder (SCM x, SCM y)
       if (yy == 0) {
        scm_num_overflow (s_remainder);
       } else {
-#if (__TURBOC__ == 1)
-       long z = SCM_INUM (x) % (yy < 0 ? -yy : yy);
-#else
        long z = SCM_INUM (x) % yy;
-#endif
        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);
     }
@@ -279,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)
 {
@@ -290,11 +332,7 @@ scm_modulo (SCM x, SCM y)
       if (yy == 0) {
        scm_num_overflow (s_modulo);
       } else {
-#if (__TURBOC__ == 1)
-       long z = ((yy < 0) ? -xx : xx) % yy;
-#else
        long z = xx % yy;
-#endif
        return SCM_MAKINUM (((yy < 0) ? (z > 0) : (z < 0)) ? z + yy : z);
       }
     } else if (SCM_BIGP (y)) {
@@ -326,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)
 {
@@ -439,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)
 {
@@ -649,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);
 }
 
@@ -708,7 +752,8 @@ SCM_DEFINE1 (scm_logand, "logand", scm_tc7_asubr,
             "Example:\n"
             "@lisp\n"
             "(number->string (logand #b1100 #b1010) 2)\n"
-            "   @result{} \"1000\"")
+            "   @result{} \"1000\"\n"
+            "@end lisp")
 #define FUNC_NAME s_scm_logand
 {
   long int nn1;
@@ -833,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, 
@@ -1010,34 +1055,42 @@ SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0,
            "@end example")
 #define FUNC_NAME s_scm_logbit_p
 {
-  SCM_ASSERT(SCM_INUMP(index) && SCM_INUM(index) >= 0, index, SCM_ARG1, FUNC_NAME);
-#ifdef SCM_BIGDIG
-  if SCM_NINUMP(j) {
-    SCM_ASSERT(SCM_BIGP (j), j, SCM_ARG2, FUNC_NAME);
-    if (SCM_NUMDIGS(j) * SCM_BITSPERDIG < SCM_INUM(index)) return SCM_BOOL_F;
-    else if SCM_BIGSIGN(j) {
+  unsigned long int iindex;
+
+  SCM_VALIDATE_INUM_MIN (SCM_ARG1, index, 0);
+  iindex = (unsigned long int) SCM_INUM (index);
+
+  if (SCM_INUMP (j)) {
+    return SCM_BOOL ((1L << iindex) & SCM_INUM (j));
+  } else if (SCM_BIGP (j)) {
+    if (SCM_NUMDIGS (j) * SCM_BITSPERDIG < iindex) {
+      return SCM_BOOL_F;
+    } else if (SCM_BIGSIGN (j)) {
       long num = -1;
       scm_sizet i = 0;
-      SCM_BIGDIG *x = SCM_BDIGITS(j);
-      scm_sizet nx = SCM_INUM(index)/SCM_BITSPERDIG;
-      while (!0) {
+      SCM_BIGDIG * x = SCM_BDIGITS (j);
+      scm_sizet nx = iindex / SCM_BITSPERDIG;
+      while (1) {
        num += x[i];
-       if (nx==i++)
-         return ((1L << (SCM_INUM(index)%SCM_BITSPERDIG)) & num) ? SCM_BOOL_F : SCM_BOOL_T;
-       if (num < 0) num = -1;
-       else num = 0;
+       if (nx == i++) {
+         return SCM_BOOL (((1L << (iindex % SCM_BITSPERDIG)) & num) == 0);
+       } else if (num < 0) {
+         num = -1;
+       } else {
+         num = 0;
+       }
       }
+    } else {
+      return SCM_BOOL (SCM_BDIGITS (j) [iindex / SCM_BITSPERDIG]
+                      & (1L << (iindex % SCM_BITSPERDIG)));
     }
-    else return (SCM_BDIGITS(j)[SCM_INUM(index)/SCM_BITSPERDIG] &
-                (1L << (SCM_INUM(index)%SCM_BITSPERDIG))) ? SCM_BOOL_T : SCM_BOOL_F;
+  } else {
+    SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
   }
-#else
-  SCM_ASSERT(SCM_INUMP(j), j, SCM_ARG2, FUNC_NAME);
-#endif
-  return ((1L << SCM_INUM(index)) & SCM_INUM(j)) ? SCM_BOOL_T : SCM_BOOL_F;
 }
 #undef FUNC_NAME
 
+
 SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0, 
             (SCM n),
            "Returns the integer which is the 2s-complement of the integer argument.\n\n"
@@ -1047,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);
@@ -1097,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
 {
@@ -1152,7 +1205,7 @@ SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
 }
 #undef FUNC_NAME
 
-/* GJB:FIXME: do not use SCMs as integers! */
+
 SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
             (SCM n, SCM start, SCM end),
            "Returns the integer composed of the @var{start} (inclusive) through\n"
@@ -1167,25 +1220,57 @@ SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
            "@end lisp")
 #define FUNC_NAME s_scm_bit_extract
 {
-  int istart, iend;
-  SCM_VALIDATE_INUM (1,n);
+  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));
-#ifdef SCM_BIGDIG
-  if (SCM_NINUMP (n))
-    return
-      scm_logand (scm_difference (scm_integer_expt (SCM_MAKINUM (2),
-                                                   SCM_MAKINUM (iend - istart)),
-                                 SCM_MAKINUM (1L)),
-                 scm_ash (n, SCM_MAKINUM (-istart)));
-#else
-  SCM_VALIDATE_INUM (1,n);
-#endif
-  return SCM_MAKINUM ((SCM_INUM (n) >> istart) & ((1L << (iend - istart)) - 1));
+
+  if (SCM_INUMP (n)) {
+    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)) {
+  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);
+  }
 }
 #undef FUNC_NAME
 
+
 static const char scm_logtab[] = {
   0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
 };
@@ -1207,30 +1292,35 @@ SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
            "@end lisp")
 #define FUNC_NAME s_scm_logcount
 {
-  register unsigned long c = 0;
-  register long nn;
-#ifdef SCM_BIGDIG
-  if (SCM_NINUMP (n))
-    {
-      scm_sizet i;
-      SCM_BIGDIG *ds, d;
-      SCM_VALIDATE_BIGINT (1,n);
-      if (SCM_BIGSIGN (n))
-       return scm_logcount (scm_difference (SCM_MAKINUM (-1L), n));
-      ds = SCM_BDIGITS (n);
-      for (i = SCM_NUMDIGS (n); i--;)
-       for (d = ds[i]; d; d >>= 4)
+  if (SCM_INUMP (n)) {
+    unsigned long int c = 0;
+    long int nn = SCM_INUM (n);
+    if (nn < 0) {
+      nn = -1 - nn;
+    };
+    while (nn) {
+      c += scm_logtab[15 & nn];
+      nn >>= 4;
+    };
+    return SCM_MAKINUM (c);
+  } else if (SCM_BIGP (n)) {
+    if (SCM_BIGSIGN (n)) {
+      return scm_logcount (scm_difference (SCM_MAKINUM (-1L), n));
+    } else {
+      unsigned long int c = 0;
+      scm_sizet i = SCM_NUMDIGS (n);
+      SCM_BIGDIG * ds = SCM_BDIGITS (n);
+      while (i--) {
+       SCM_BIGDIG d;
+       for (d = ds[i]; d; d >>= 4) {
          c += scm_logtab[15 & d];
+       }
+      }
       return SCM_MAKINUM (c);
     }
-#else
-  SCM_VALIDATE_INUM (1,n);
-#endif
-  if ((nn = SCM_INUM (n)) < 0)
-    nn = -1 - nn;
-  for (; nn; nn >>= 4)
-    c += scm_logtab[15 & nn];
-  return SCM_MAKINUM (c);
+  } else {
+    SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
+  }
 }
 #undef FUNC_NAME
 
@@ -1253,36 +1343,38 @@ SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
            "@end lisp")
 #define FUNC_NAME s_scm_integer_length
 {
-  register unsigned long c = 0;
-  register long nn;
-  unsigned int l = 4;
-#ifdef SCM_BIGDIG
-  if (SCM_NINUMP (n))
-    {
-      SCM_BIGDIG *ds, d;
-      SCM_VALIDATE_BIGINT (1,n);
-      if (SCM_BIGSIGN (n))
-       return scm_integer_length (scm_difference (SCM_MAKINUM (-1L), n));
-      ds = SCM_BDIGITS (n);
-      d = ds[c = SCM_NUMDIGS (n) - 1];
-      for (c *= SCM_BITSPERDIG; d; d >>= 4)
-       {
-         c += 4;
-         l = scm_ilentab[15 & d];
-       }
-      return SCM_MAKINUM (c - 4 + l);
-    }
-#else
-  SCM_VALIDATE_INUM (1,n);
-#endif
-  if ((nn = SCM_INUM (n)) < 0)
-    nn = -1 - nn;
-  for (; nn; nn >>= 4)
-    {
+  if (SCM_INUMP (n)) {
+    unsigned long int c = 0;
+    unsigned int l = 4;
+    long int nn = SCM_INUM (n);
+    if (nn < 0) {
+      nn = -1 - nn;
+    };
+    while (nn) {
       c += 4;
-      l = scm_ilentab[15 & nn];
+      l = scm_ilentab [15 & nn];
+      nn >>= 4;
+    };
+    return SCM_MAKINUM (c - 4 + l);
+  } else if (SCM_BIGP (n)) {
+    if (SCM_BIGSIGN (n)) {
+      return scm_integer_length (scm_difference (SCM_MAKINUM (-1L), n));
+    } else {
+      unsigned long int digs = SCM_NUMDIGS (n) - 1;
+      unsigned long int c = digs * SCM_BITSPERDIG;
+      unsigned int l = 4;
+      SCM_BIGDIG * ds = SCM_BDIGITS (n);
+      SCM_BIGDIG d = ds [digs];
+      while (d) {
+       c += 4;
+       l = scm_ilentab [15 & d];
+       d >>= 4;
+      };
+      return SCM_MAKINUM (c - 4 + l);
     }
-  return SCM_MAKINUM (c - 4 + l);
+  } else {
+    SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
+  }
 }
 #undef FUNC_NAME
 
@@ -1297,12 +1389,11 @@ scm_mkbig (scm_sizet nlen, int sign)
   /* Cast to long int to avoid signed/unsigned comparison warnings.  */
   if ((( ((long int) nlen) << SCM_BIGSIZEFIELD) >> SCM_BIGSIZEFIELD)
       != (long int) nlen)
-    scm_wta (SCM_MAKINUM (nlen), (char *) SCM_NALLOC, s_bignum);
+    scm_memory_error (s_bignum);
   
   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;
@@ -1321,7 +1412,7 @@ scm_big2inum (SCM b, scm_sizet l)
       if (SCM_POSFIXABLE (num))
        return SCM_MAKINUM (num);
     }
-  else if (SCM_UNEGFIXABLE (num))
+  else if (num <= -SCM_MOST_NEGATIVE_FIXNUM)
     return SCM_MAKINUM (-num);
   return b;
 }
@@ -1334,17 +1425,17 @@ scm_adjbig (SCM b, scm_sizet nlen)
 {
   scm_sizet nsiz = nlen;
   if (((nsiz << SCM_BIGSIZEFIELD) >> SCM_BIGSIZEFIELD) != nlen)
-    scm_wta (scm_ulong2num (nsiz), (char *) SCM_NALLOC, s_adjbig);
+    scm_memory_error (s_adjbig);
 
   SCM_DEFER_INTS;
   {
     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;
@@ -1442,7 +1533,7 @@ scm_long_long2big (long_long n)
     }
   return ans;
 }
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 SCM
@@ -2153,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)
@@ -2177,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);
@@ -2194,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;
@@ -2248,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
@@ -2618,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;
@@ -2675,7 +2766,7 @@ scm_istr2flo (char *str, long len, long radix)
          return SCM_BOOL_F;    /* not `real' */
        if (SCM_SLOPPY_COMPLEXP (second))
          return SCM_BOOL_F;    /* not `real' */
-       tmp = SCM_REALPART (second);
+       tmp = SCM_REAL_VALUE (second);
        return scm_make_complex (res * cos (tmp), res * sin (tmp));
       }
     default:
@@ -2694,7 +2785,7 @@ scm_istr2flo (char *str, long len, long radix)
     return SCM_BOOL_F;         /* not `ureal' */
   if (SCM_SLOPPY_COMPLEXP (second))
     return SCM_BOOL_F;         /* not `ureal' */
-  tmp = SCM_REALPART (second);
+  tmp = SCM_REAL_VALUE (second);
   if (tmp < 0.0)
     return SCM_BOOL_F;         /* not `ureal' */
   return scm_make_complex (res, (lead_sgn * tmp));
@@ -2774,42 +2865,54 @@ 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);
 }
 #undef FUNC_NAME
 /*** END strs->nums ***/
 
+
 SCM
 scm_make_real (double x)
 {
   SCM z;
-  SCM_NEWREAL (z, x);
+  SCM_NEWCELL2 (z);
+  SCM_SET_CELL_TYPE (z, scm_tc16_real);
+  SCM_REAL_VALUE (z) = x;
   return z;
 }
 
+
 SCM
 scm_make_complex (double x, double y)
 {
-  SCM z;
-  SCM_NEWCOMPLEX (z, x, y);
-  return z;
+  if (y == 0.0) {
+    return scm_make_real (x);
+  } else {
+    SCM z;
+    SCM_NEWSMOB (z, scm_tc16_complex, scm_must_malloc (2L * sizeof (double), "complex"));
+    SCM_COMPLEX_REAL (z) = x;
+    SCM_COMPLEX_IMAG (z) = y;
+    return z;
+  }
 }
 
+
 SCM
 scm_bigequal (SCM x, SCM y)
 {
@@ -2836,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));
@@ -2851,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)) {
@@ -2877,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;
@@ -2891,7 +3006,7 @@ SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
     return SCM_BOOL_F;
   if (SCM_SLOPPY_COMPLEXP (x))
     return SCM_BOOL_F;
-  r = SCM_REALPART (x);
+  r = SCM_REAL_VALUE (x);
   if (r == floor (r))
     return SCM_BOOL_T;
   return SCM_BOOL_F;
@@ -2901,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));
@@ -2910,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)
 {
@@ -2978,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)
 {
@@ -3020,41 +3138,64 @@ scm_less_p (SCM x, SCM y)
 }
 
 
-SCM_DEFINE1 (scm_gr_p, ">", scm_tc7_rpsubr,
-             (SCM x, SCM y),
           "Return #t if the list of parameters is monotonically\n"
-            "increasing.")
+SCM_GPROC1 (s_scm_gr_p, ">", scm_tc7_rpsubr, scm_gr_p, g_gr_p);
+/* "Return @code{#t} if the list of parameters is monotonically\n"
* "decreasing."
+ */
 #define FUNC_NAME s_scm_gr_p
+SCM
+scm_gr_p (SCM x, SCM y)
 {
-  return scm_less_p (y, x);
+  if (!SCM_NUMBERP (x))
+    SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG1, FUNC_NAME);
+  else if (!SCM_NUMBERP (y))
+    SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG2, FUNC_NAME);
+  else
+    return scm_less_p (y, x);
 }
 #undef FUNC_NAME
 
 
-SCM_DEFINE1 (scm_leq_p, "<=", scm_tc7_rpsubr,
-             (SCM x, SCM y),
           "Return #t if the list of parameters is monotonically\n"
-            "non-decreasing.")
+SCM_GPROC1 (s_scm_leq_p, "<=", scm_tc7_rpsubr, scm_leq_p, g_leq_p);
+/* "Return @code{#t} if the list of parameters is monotonically\n"
* "non-decreasing."
+ */
 #define FUNC_NAME s_scm_leq_p
+SCM
+scm_leq_p (SCM x, SCM y)
 {
-  return SCM_BOOL_NOT (scm_less_p (y, x));
+  if (!SCM_NUMBERP (x))
+    SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG1, FUNC_NAME);
+  else if (!SCM_NUMBERP (y))
+    SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG2, FUNC_NAME);
+  else
+    return SCM_BOOL_NOT (scm_less_p (y, x));
 }
 #undef FUNC_NAME
 
 
-SCM_DEFINE1 (scm_geq_p, ">=", scm_tc7_rpsubr,
-             (SCM x, SCM y),
           "Return #t if the list of parameters is monotonically\n"
-            "non-increasing.")
+SCM_GPROC1 (s_scm_geq_p, ">=", scm_tc7_rpsubr, scm_geq_p, g_geq_p);
+/* "Return @code{#t} if the list of parameters is monotonically\n"
* "non-increasing."
+ */
 #define FUNC_NAME s_scm_geq_p
+SCM
+scm_geq_p (SCM x, SCM y)
 {
+  if (!SCM_NUMBERP (x))
+    SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG1, FUNC_NAME);
+  else if (!SCM_NUMBERP (y))
+    SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG2, FUNC_NAME);
+  else
   return SCM_BOOL_NOT (scm_less_p (x, y));
 }
 #undef FUNC_NAME
 
 
 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)
 {
@@ -3074,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)
 {
@@ -3091,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)
 {
@@ -3108,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)
 {
@@ -3165,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)
 {
@@ -3222,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)
 {
@@ -3327,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)
 {
@@ -3453,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)
 {
@@ -3600,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)
 {
@@ -3756,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)
 {
@@ -3767,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)
 {
@@ -3778,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)
 {
@@ -3789,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)
 {
@@ -3801,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)
 {
@@ -3815,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)
 {
@@ -3824,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
@@ -3900,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;
@@ -3913,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)
 {
@@ -3944,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)
 {
@@ -3963,14 +4162,31 @@ 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)
 {
   if (SCM_INUMP (z)) {
-    return scm_abs (z);
+    long int zz = SCM_INUM (z);
+    if (zz >= 0) {
+      return z;
+    } else if (SCM_POSFIXABLE (-zz)) {
+      return SCM_MAKINUM (-zz);
+    } else {
+#ifdef SCM_BIGDIG
+      return scm_long2big (-zz);
+#else
+      scm_num_overflow (s_magnitude);
+#endif
+    }
   } else if (SCM_BIGP (z)) {
-    return scm_abs (z);
+    if (!SCM_BIGSIGN (z)) {
+      return z;
+    } else {
+      return scm_copybig (z, 0);
+    }
   } else if (SCM_REALP (z)) {
     return scm_make_real (fabs (SCM_REAL_VALUE (z)));
   } else if (SCM_COMPLEXP (z)) {
@@ -3984,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)
 {
@@ -4012,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)) {
@@ -4122,7 +4339,7 @@ scm_long_long2num (long_long sl)
     }
 }
 
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 SCM
@@ -4241,7 +4458,7 @@ scm_num2long_long (SCM num, char *pos, const char *s_caller)
   }
 }
 
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 unsigned long
@@ -4285,9 +4502,19 @@ 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_NEWREAL (scm_flo0, 0.0);
+  scm_flo0 = scm_make_real (0.0);
 #ifdef DBL_DIG
   scm_dblprec = (DBL_DIG > 20) ? 20 : DBL_DIG;
 #else
@@ -4305,7 +4532,9 @@ scm_init_numbers ()
     scm_dblprec = scm_dblprec - 1;
   }
 #endif /* DBL_DIG */
+#ifndef SCM_MAGIC_SNARFER
 #include "libguile/numbers.x"
+#endif
 }
 
 /*