Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / numbers.c
index a00a5c2..0452e43 100644 (file)
 
  */
 
-/* tell glibc (2.3) to give prototype for C99 trunc(), csqrt(), etc */
-#define _GNU_SOURCE
-
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
@@ -162,20 +159,25 @@ xisnan (double x)
 #endif
 }
 
+#if defined (GUILE_I)
+#if HAVE_COMPLEX_DOUBLE
 
 /* For an SCM object Z which is a complex number (ie. satisfies
    SCM_COMPLEXP), return its value as a C level "complex double". */
 #define SCM_COMPLEX_VALUE(z)                                    \
-  (SCM_COMPLEX_REAL (z) + _Complex_I * SCM_COMPLEX_IMAG (z))
+  (SCM_COMPLEX_REAL (z) + GUILE_I * SCM_COMPLEX_IMAG (z))
+
+static inline SCM scm_from_complex_double (complex double z) SCM_UNUSED;
 
 /* Convert a C "complex double" to an SCM value. */
-#if HAVE_COMPLEX_DOUBLE
-static SCM
+static inline SCM
 scm_from_complex_double (complex double z)
 {
   return scm_c_make_rectangular (creal (z), cimag (z));
 }
+
 #endif /* HAVE_COMPLEX_DOUBLE */
+#endif /* GUILE_I */
 
 \f
 
@@ -5326,8 +5328,9 @@ scm_c_make_rectangular (double re, double im)
   else
     {
       SCM z;
-      SCM_NEWSMOB (z, scm_tc16_complex, scm_gc_malloc (sizeof (scm_t_complex),
-                                                      "complex"));
+      SCM_NEWSMOB (z, scm_tc16_complex,
+                  scm_gc_malloc_pointerless (sizeof (scm_t_complex),
+                                             "complex"));
       SCM_COMPLEX_REAL (z) = re;
       SCM_COMPLEX_IMAG (z) = im;
       return z;
@@ -5335,13 +5338,13 @@ scm_c_make_rectangular (double re, double im)
 }
 
 SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
-            (SCM real, SCM imaginary),
-           "Return a complex number constructed of the given @var{real} and\n"
-           "@var{imaginary} parts.")
+            (SCM real_part, SCM imaginary_part),
+           "Return a complex number constructed of the given @var{real-part} "
+           "and @var{imaginary-part} parts.")
 #define FUNC_NAME s_scm_make_rectangular
 {
   struct dpair xy;
-  scm_two_doubles (real, imaginary, FUNC_NAME, &xy);
+  scm_two_doubles (real_part, imaginary_part, FUNC_NAME, &xy);
   return scm_c_make_rectangular (xy.x, xy.y);
 }
 #undef FUNC_NAME
@@ -5594,8 +5597,18 @@ SCM_DEFINE (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
 #undef FUNC_NAME
 
 SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0, 
-            (SCM x, SCM err),
-           "Return an exact number that is within @var{err} of @var{x}.")
+            (SCM x, SCM eps),
+           "Returns the @emph{simplest} rational number differing\n"
+           "from @var{x} by no more than @var{eps}.\n"
+           "\n"
+           "As required by @acronym{R5RS}, @code{rationalize} only returns an\n"
+           "exact result when both its arguments are exact.  Thus, you might need\n"
+           "to use @code{inexact->exact} on the arguments.\n"
+           "\n"
+           "@lisp\n"
+           "(rationalize (inexact->exact 1.2) 1/100)\n"
+           "@result{} 6/5\n"
+           "@end lisp")
 #define FUNC_NAME s_scm_rationalize
 {
   if (SCM_I_INUMP (x))
@@ -5627,7 +5640,7 @@ SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
         converges after less than a dozen iterations.
       */
 
-      err = scm_abs (err);
+      eps = scm_abs (eps);
       while (++i < 1000000)
        {
          a = scm_sum (scm_product (a1, tt), a2);    /* a = a1*tt + a2 */
@@ -5635,11 +5648,11 @@ SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
          if (scm_is_false (scm_zero_p (b)) &&         /* b != 0 */
              scm_is_false 
              (scm_gr_p (scm_abs (scm_difference (ex, scm_divide (a, b))),
-                        err)))                      /* abs(x-a/b) <= err */
+                        eps)))                      /* abs(x-a/b) <= eps */
            {
              SCM res = scm_sum (int_part, scm_divide (a, b));
              if (scm_is_false (scm_exact_p (x))
-                 || scm_is_false (scm_exact_p (err)))
+                 || scm_is_false (scm_exact_p (eps)))
                return scm_exact_to_inexact (res);
              else
                return res;
@@ -6011,7 +6024,7 @@ SCM_DEFINE (scm_log, "log", 1, 0, 0,
 {
   if (SCM_COMPLEXP (z))
     {
-#if HAVE_COMPLEX_DOUBLE && HAVE_CLOG
+#if HAVE_COMPLEX_DOUBLE && HAVE_CLOG && defined (SCM_COMPLEX_VALUE)
       return scm_from_complex_double (clog (SCM_COMPLEX_VALUE (z)));
 #else
       double re = SCM_COMPLEX_REAL (z);
@@ -6045,7 +6058,7 @@ SCM_DEFINE (scm_log10, "log10", 1, 0, 0,
       /* Mingw has clog() but not clog10().  (Maybe it'd be worth using
          clog() and a multiply by M_LOG10E, rather than the fallback
          log10+hypot+atan2.)  */
-#if HAVE_COMPLEX_DOUBLE && HAVE_CLOG10
+#if HAVE_COMPLEX_DOUBLE && HAVE_CLOG10 && defined (SCM_COMPLEX_VALUE)
       return scm_from_complex_double (clog10 (SCM_COMPLEX_VALUE (z)));
 #else
       double re = SCM_COMPLEX_REAL (z);
@@ -6077,7 +6090,7 @@ SCM_DEFINE (scm_exp, "exp", 1, 0, 0,
 {
   if (SCM_COMPLEXP (z))
     {
-#if HAVE_COMPLEX_DOUBLE && HAVE_CEXP
+#if HAVE_COMPLEX_DOUBLE && HAVE_CEXP && defined (SCM_COMPLEX_VALUE)
       return scm_from_complex_double (cexp (SCM_COMPLEX_VALUE (z)));
 #else
       return scm_c_make_polar (exp (SCM_COMPLEX_REAL (z)),
@@ -6111,7 +6124,7 @@ SCM_DEFINE (scm_sqrt, "sqrt", 1, 0, 0,
 {
   if (SCM_COMPLEXP (x))
     {
-#if HAVE_COMPLEX_DOUBLE && HAVE_USABLE_CSQRT
+#if HAVE_COMPLEX_DOUBLE && HAVE_USABLE_CSQRT && defined (SCM_COMPLEX_VALUE)
       return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (x)));
 #else
       double re = SCM_COMPLEX_REAL (x);