* Cleaned up memory error signalling.
[bpt/guile.git] / libguile / numbers.c
index bd40ae6..6d3e808 100644 (file)
@@ -70,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) / 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))
@@ -157,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);
   }
@@ -244,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)) {
@@ -289,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)) {
@@ -1009,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"
@@ -1151,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"
@@ -1167,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
 };
@@ -1206,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
 
@@ -1252,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
 
@@ -1296,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;
@@ -1320,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;
 }
@@ -1333,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;
   {
@@ -1441,7 +1464,7 @@ scm_long_long2big (long_long n)
     }
   return ans;
 }
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 SCM
@@ -2674,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:
@@ -2693,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));
@@ -2793,22 +2816,33 @@ SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
 #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)
 {
@@ -2890,7 +2924,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;
@@ -3967,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)) {
@@ -4121,7 +4170,7 @@ scm_long_long2num (long_long sl)
     }
 }
 
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 SCM
@@ -4240,7 +4289,7 @@ scm_num2long_long (SCM num, char *pos, const char *s_caller)
   }
 }
 
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 unsigned long
@@ -4286,7 +4335,7 @@ 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