commit b9128cbee229e6b0534ebb42abebc03242501887
parent 51b19792042a7cc65292698f0a4c947f426ffcc6
Author: Andres Navarro <canavarro82@gmail.com>
Date: Wed, 4 May 2011 17:50:28 -0300
Moved code printing doubles to kreal in preparation for new double printing code.
Diffstat:
3 files changed, 49 insertions(+), 4 deletions(-)
diff --git a/src/kreal.c b/src/kreal.c
@@ -46,6 +46,7 @@ double kbigint_to_double(Bigint *bigint)
/* bigrat is rooted */
double kbigrat_to_double(klisp_State *K, Bigrat *bigrat)
{
+ /* TODO: check rounding in extreme cases */
TValue tv_rem = kbigrat_copy(K, gc2bigrat(bigrat));
krooted_tvs_push(K, tv_rem);
Bigrat *rem = tv2bigrat(tv_rem);
@@ -125,3 +126,22 @@ TValue kexact_to_inexact(klisp_State *K, TValue n)
return KUNDEF;
}
}
+
+/*
+** read/write interface
+*/
+
+/* TEMP: this is a stub for now, always return sufficiently large
+ number */
+int32_t kdouble_print_size(TValue tv_double)
+{
+ UNUSED(tv_double);
+ return 1024;
+}
+
+void kdouble_print_string(klisp_State *K, TValue tv_double,
+ char *buf, int32_t limit)
+{
+ sprintf(buf, "%f", dvalue(tv_double));
+ return;
+}
diff --git a/src/kreal.h b/src/kreal.h
@@ -18,4 +18,12 @@
TValue kexact_to_inexact(klisp_State *K, TValue n);
+
+/*
+** read/write interface
+*/
+int32_t kdouble_print_size(TValue tv_double);
+void kdouble_print_string(klisp_State *K, TValue tv_double,
+ char *buf, int32_t limit);
+
#endif
diff --git a/src/kwrite.c b/src/kwrite.c
@@ -8,6 +8,7 @@
#include <stdlib.h>
#include <assert.h>
#include <inttypes.h>
+#include <string.h>
#include "kwrite.h"
#include "kobject.h"
@@ -91,6 +92,23 @@ void kw_print_bigrat(klisp_State *K, TValue bigrat)
krooted_tvs_pop(K);
}
+void kw_print_double(klisp_State *K, TValue tv_double)
+{
+ int32_t size = kdouble_print_size(tv_double);
+ krooted_tvs_push(K, tv_double);
+ /* here we are using 1 byte extra, because size already includes
+ 1 for the terminator, but better be safe than sorry */
+ TValue buf_str = kstring_new_s(K, size);
+ krooted_tvs_push(K, buf_str);
+
+ char *buf = kstring_buf(buf_str);
+ kdouble_print_string(K, tv_double, buf, size);
+ kw_printf(K, "%s", buf);
+
+ krooted_tvs_pop(K);
+ krooted_tvs_pop(K);
+}
+
/*
** Helper for printing strings (correcly escapes backslashes and
** double quotes & prints embedded '\0's). It includes the surrounding
@@ -287,11 +305,10 @@ void kwrite_simple(klisp_State *K, TValue obj)
case K_TIINF:
kw_printf(K, "#i%cinfinity", tv_equal(obj, KIPINF)? '+' : '-');
break;
- case K_TDOUBLE:
- /* TODO investigate this further */
- /* LOCALE, significant digits, etc */
- kw_printf(K, "%f", dvalue(obj));
+ case K_TDOUBLE: {
+ kw_print_double(K, obj);
break;
+ }
case K_TRWNPV:
/* ASK John/TEMP: until John tells me what should this be... */
kw_printf(K, "#real");