* Cleaned up memory error signalling.
[bpt/guile.git] / libguile / numbers.c
index 3624f1a..6d3e808 100644 (file)
@@ -47,7 +47,6 @@
 #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"
@@ -71,13 +70,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))
@@ -158,6 +170,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);
   }
@@ -245,11 +259,7 @@ 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)) {
@@ -290,11 +300,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)) {
@@ -1010,34 +1016,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"
@@ -1152,7 +1166,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"
@@ -1168,24 +1182,25 @@ SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
 #define FUNC_NAME s_scm_bit_extract
 {
   int istart, iend;
-  SCM_VALIDATE_INUM (1,n);
   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)) {
+    return SCM_MAKINUM ((SCM_INUM (n) >> istart) & ((1L << (iend - istart)) - 1));
+  } 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)));
+  } 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 +1222,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 +1273,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,7 +1319,7 @@ 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;
@@ -1321,7 +1343,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,7 +1356,7 @@ 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;
   {
@@ -1442,7 +1464,7 @@ scm_long_long2big (long_long n)
     }
   return ans;
 }
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 SCM
@@ -1689,16 +1711,8 @@ scm_mulbig (SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn)
 }
 
 
-/* Sun's compiler complains about the fact that this function has an
-   ANSI prototype in numbers.h, but a K&R declaration here, and the
-   two specify different promotions for the third argument.  I'm going
-   to turn this into an ANSI declaration, and see if anyone complains
-   about it not being K&R.  */
-
 unsigned int
-scm_divbigdig (SCM_BIGDIG * ds,
-              scm_sizet h,
-              SCM_BIGDIG div)
+scm_divbigdig (SCM_BIGDIG * ds, scm_sizet h, SCM_BIGDIG div)
 {
   register unsigned long t2 = 0;
   while (h--)
@@ -2200,8 +2214,10 @@ big2str (SCM b, unsigned int radix)
 
 
 SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
-            (SCM x, SCM radix),
-           "")
+            (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.")
 #define FUNC_NAME s_scm_number_to_string
 {
   int base;
@@ -2214,17 +2230,17 @@ SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
     SCM_ASSERT_RANGE (2, radix, base >= 2);
   }
 
-  if (SCM_INUMP (x)) {
+  if (SCM_INUMP (n)) {
     char num_buf [SCM_INTBUFLEN];
-    scm_sizet length = scm_iint2str (SCM_INUM (x), base, num_buf);
+    scm_sizet length = scm_iint2str (SCM_INUM (n), base, num_buf);
     return scm_makfromstr (num_buf, length, 0);
-  } else if (SCM_BIGP (x)) {
-    return big2str (x, (unsigned int) base);
-  } else if (SCM_INEXACTP (x)) {
+  } else if (SCM_BIGP (n)) {
+    return big2str (n, (unsigned int) base);
+  } else if (SCM_INEXACTP (n)) {
     char num_buf [SCM_FLOBUFLEN];
-    return scm_makfromstr (num_buf, iflo2str (x, num_buf), 0);
+    return scm_makfromstr (num_buf, iflo2str (n, num_buf), 0);
   } else {
-    SCM_WRONG_TYPE_ARG (1, x);
+    SCM_WRONG_TYPE_ARG (1, n);
   }
 }
 #undef FUNC_NAME
@@ -2681,7 +2697,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:
@@ -2700,7 +2716,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));
@@ -2778,38 +2794,55 @@ scm_istring2number (char *str, long len, long radix)
 
 
 SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
-            (SCM str, SCM radix),
-           "")
+            (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)") 
 #define FUNC_NAME s_scm_string_to_number
 {
   SCM answer;
   int base;
-  SCM_VALIDATE_ROSTRING (1,str);
+  SCM_VALIDATE_ROSTRING (1,string);
   SCM_VALIDATE_INUM_MIN_DEF_COPY (2,radix,2,10,base);
-  answer = scm_istring2number (SCM_ROCHARS (str),
-                              SCM_ROLENGTH (str),
+  answer = scm_istring2number (SCM_ROCHARS (string),
+                              SCM_ROLENGTH (string),
                                base);
-  return scm_return_first (answer, str);
+  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)
 {
@@ -2839,46 +2872,45 @@ SCM_REGISTER_PROC (s_number_p, "number?", 1, 0, 0, scm_number_p);
 
 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.")
 #define FUNC_NAME s_scm_number_p
 {
-  if (SCM_INUMP (x))
-    return SCM_BOOL_T;
-  if (SCM_NUMP (x))
-    return SCM_BOOL_T;
-  return SCM_BOOL_F;
+  return SCM_BOOL (SCM_NUMBERP (x));
 }
 #undef FUNC_NAME
 
 
-
 SCM_REGISTER_PROC (s_real_p, "real?", 1, 0, 0, scm_real_p);
 
-
 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.")
 #define FUNC_NAME s_scm_real_p
 {
-  if (SCM_INUMP (x))
+  if (SCM_INUMP (x)) {
     return SCM_BOOL_T;
-  if (SCM_IMP (x))
+  } else if (SCM_IMP (x)) {
     return SCM_BOOL_F;
-  if (SCM_SLOPPY_REALP (x))
+  } else if (SCM_SLOPPY_REALP (x)) {
     return SCM_BOOL_T;
-#ifdef SCM_BIGDIG
-  if (SCM_BIGP (x))
+  } else if (SCM_BIGP (x)) {
     return SCM_BOOL_T;
-#endif
-  return SCM_BOOL_F;
+  } else {
+    return SCM_BOOL_F;
+  }
 }
 #undef FUNC_NAME
 
 
-
 SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0, 
             (SCM x),
-           "")
+           "Return #t if X is an integer number, #f else.")
 #define FUNC_NAME s_scm_integer_p
 {
   double r;
@@ -2886,15 +2918,13 @@ SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
     return SCM_BOOL_T;
   if (SCM_IMP (x))
     return SCM_BOOL_F;
-#ifdef SCM_BIGDIG
   if (SCM_BIGP (x))
     return SCM_BOOL_T;
-#endif
   if (!SCM_SLOPPY_INEXACTP (x))
     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;
@@ -2902,10 +2932,9 @@ SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
 #undef FUNC_NAME
 
 
-
 SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0, 
             (SCM x),
-           "")
+           "Return #t if X is an inexact number, #f else.")
 #define FUNC_NAME s_scm_inexact_p
 {
   return SCM_BOOL (SCM_INEXACTP (x));
@@ -3026,7 +3055,8 @@ 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.")
 #define FUNC_NAME s_scm_gr_p
 {
   return scm_less_p (y, x);
@@ -3036,7 +3066,8 @@ SCM_DEFINE1 (scm_gr_p, ">", scm_tc7_rpsubr,
 
 SCM_DEFINE1 (scm_leq_p, "<=", scm_tc7_rpsubr,
              (SCM x, SCM y),
-            "")
+            "Return #t if the list of parameters is monotonically\n"
+            "non-decreasing.")
 #define FUNC_NAME s_scm_leq_p
 {
   return SCM_BOOL_NOT (scm_less_p (y, x));
@@ -3046,7 +3077,8 @@ SCM_DEFINE1 (scm_leq_p, "<=", scm_tc7_rpsubr,
 
 SCM_DEFINE1 (scm_geq_p, ">=", scm_tc7_rpsubr,
              (SCM x, SCM y),
-            "")
+            "Return #t if the list of parameters is monotonically\n"
+            "non-increasing.")
 #define FUNC_NAME s_scm_geq_p
 {
   return SCM_BOOL_NOT (scm_less_p (x, y));
@@ -3877,7 +3909,7 @@ scm_two_doubles (SCM z1, SCM z2, const char *sstring, struct dpair *xy)
 
 SCM_DEFINE (scm_sys_expt, "$expt", 2, 0, 0,
             (SCM z1, SCM z2),
-           "")
+           "") 
 #define FUNC_NAME s_scm_sys_expt
 {
   struct dpair xy;
@@ -3887,7 +3919,6 @@ SCM_DEFINE (scm_sys_expt, "$expt", 2, 0, 0,
 #undef FUNC_NAME
 
 
-
 SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0,
             (SCM z1, SCM z2),
            "")
@@ -3900,14 +3931,14 @@ SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0,
 #undef FUNC_NAME
 
 
-
 SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
-            (SCM z1, SCM z2),
-           "")
+            (SCM real, SCM imaginary),
+           "Return a complex number constructed of the given REAL and\n"
+           "IMAGINARY parts.")
 #define FUNC_NAME s_scm_make_rectangular
 {
   struct dpair xy;
-  scm_two_doubles (z1, z2, FUNC_NAME, &xy);
+  scm_two_doubles (real, imaginary, FUNC_NAME, &xy);
   return scm_make_complex (xy.x, xy.y);
 }
 #undef FUNC_NAME
@@ -3916,7 +3947,7 @@ 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).")
 #define FUNC_NAME s_scm_make_polar
 {
   struct dpair xy;
@@ -3970,9 +4001,24 @@ 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)) {
@@ -4014,7 +4060,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.")
 #define FUNC_NAME s_scm_inexact_to_exact
 {
   if (SCM_INUMP (z)) {
@@ -4124,7 +4170,7 @@ scm_long_long2num (long_long sl)
     }
 }
 
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 SCM
@@ -4243,7 +4289,7 @@ scm_num2long_long (SCM num, char *pos, const char *s_caller)
   }
 }
 
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 unsigned long
@@ -4284,34 +4330,26 @@ scm_num2ulong (SCM num, char *pos, const char *s_caller)
 }
 
 
-#ifndef DBL_DIG
-static void
-add1 (double f, double *fsum)
-{
-  *fsum = f + 1.0;
-}
-#endif
-
-
 void
 scm_init_numbers ()
 {
   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
   {                            /* determine floating point precision */
     double f = 0.1;
     double fsum = 1.0 + f;
-    while (fsum != 1.0)
-      {
+    while (fsum != 1.0) {
+      if (++scm_dblprec > 20) {
+       fsum = 1.0;
+      } else {
        f /= 10.0;
-       if (++scm_dblprec > 20)
-         break;
-       add1 (f, &fsum);
+       fsum = f + 1.0;
       }
+    }
     scm_dblprec = scm_dblprec - 1;
   }
 #endif /* DBL_DIG */