* Makefile.am: Distribute num2float.i.c.
authorMikael Djurfeldt <djurfeldt@nada.kth.se>
Sat, 22 Sep 2001 21:39:42 +0000 (21:39 +0000)
committerMikael Djurfeldt <djurfeldt@nada.kth.se>
Sat, 22 Sep 2001 21:39:42 +0000 (21:39 +0000)
* num2float.i.c: New file, multiply included by numbers.c, used
to "templatize" the float <-> num conversion routines.

* numbers.c: New functions: scm_num2float, scm_float2num,
scm_num2double, scm_double2num.

NEWS
libguile/ChangeLog
libguile/Makefile.am
libguile/num2float.i.c [new file with mode: 0644]
libguile/numbers.c

diff --git a/NEWS b/NEWS
index f29a14e..d97db2e 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1208,6 +1208,12 @@ These are conversion functions between the various ANSI C integral
 types and Scheme numbers.  NOTE: The scm_num2xxx functions don't
 accept an inexact argument.
 
+** New functions: scm_float2num, scm_double2num,
+   scm_num2float, scm_num2double.
+
+These are conversion functions between the two ANSI C float types and
+Scheme numbers.
+
 ** New number validation macros:
    SCM_NUM2{SIZE,PTRDIFF,SHORT,USHORT,INT,UINT}[_DEF]
 
index 75e2c56..a753021 100644 (file)
@@ -1,3 +1,13 @@
+2001-09-22  Mikael Djurfeldt  <mdj@linnaeus>
+
+       * Makefile.am: Distribute num2float.i.c.
+
+       * num2float.i.c: New file, multiply included by numbers.c, used
+       to "templatize" the float <-> num conversion routines.
+
+       * numbers.c: New functions: scm_num2float, scm_float2num,
+       scm_num2double, scm_double2num.
+
 2001-09-21  Rob Browning  <rlb@defaultvalue.org>
 
        * .cvsignore: really add version.h
index 09fa64a..9ead1f8 100644 (file)
@@ -111,7 +111,8 @@ install-exec-hook:
 ## compile, since they are #included.  So instead we list them here.
 ## Perhaps we can deal with them normally once the merge seems to be
 ## working.
-noinst_HEADERS = coop-threads.c coop-threads.h coop.c num2integral.i.c
+noinst_HEADERS = coop-threads.c coop-threads.h coop.c \
+                num2integral.i.c num2float.i.c
 
 libguile_la_DEPENDENCIES = @LIBLOBJS@
 libguile_la_LIBADD = @LIBLOBJS@ $(LIBLTDL)
diff --git a/libguile/num2float.i.c b/libguile/num2float.i.c
new file mode 100644 (file)
index 0000000..22efffb
--- /dev/null
@@ -0,0 +1,50 @@
+/* this file is #include'd (several times) by numbers.c */
+
+FTYPE
+NUM2FLOAT (SCM num, unsigned long int pos, const char *s_caller)
+{
+  if (SCM_INUMP (num))
+    return SCM_INUM (num);
+  else if (SCM_BIGP (num))
+    { /* bignum */
+    
+      FTYPE res = 0.0;
+      size_t l;
+
+      for (l = SCM_NUMDIGS (num); l--;)
+       res = SCM_BIGRAD * res + SCM_BDIGITS (num)[l];
+      
+      if (SCM_BIGSIGN (num))
+       res = -res;
+      
+      if (isfinite (res))
+       return res;
+      else
+       scm_out_of_range (s_caller, num);
+    }
+  else if (SCM_REALP (num))
+    return SCM_REAL_VALUE (num);
+  else
+    scm_wrong_type_arg (s_caller, pos, num);
+}
+
+SCM
+FLOAT2NUM (FTYPE n)
+{
+  SCM z;
+  SCM_NEWCELL2 (z);
+  SCM_SET_CELL_TYPE (z, scm_tc16_real);
+  SCM_REAL_VALUE (z) = n;
+  return z;
+}
+
+/* clean up */
+#undef FLOAT2NUM
+#undef NUM2FLOAT
+#undef FTYPE
+
+/*
+  Local Variables:
+  c-file-style: "gnu"
+  End:
+*/
index 1c26b91..596bacb 100644 (file)
@@ -4365,6 +4365,16 @@ scm_i_big2dbl (SCM b)
 
 #endif /* HAVE_LONG_LONGS */
 
+#define NUM2FLOAT scm_num2float
+#define FLOAT2NUM scm_float2num
+#define FTYPE float
+#include "libguile/num2float.i.c"
+
+#define NUM2FLOAT scm_num2double
+#define FLOAT2NUM scm_double2num
+#define FTYPE double
+#include "libguile/num2float.i.c"
+
 #ifdef GUILE_DEBUG
 
 #define CHECK(type, v) \