diff options
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | arith.c | 6 | ||||
-rw-r--r-- | stream.c | 14 |
3 files changed, 29 insertions, 0 deletions
@@ -1,5 +1,14 @@ 2012-03-22 Kaz Kylheku <kaz@kylheku.com> + * arith.c (int_flo): If sprintf produces something + that doesn't begin with a digit, it's most likely NaN or Inf. + We can turn that into an exception. + + * stream.c (vformat): If sprintf produces a non-number, + turn it into the printed representation #<bad-float>. + +2012-03-22 Kaz Kylheku <kaz@kylheku.com> + * arith.c (to_float): New static function. (divi): Uses to_float. (zerop, gt, lt, ge, le, expt): Floating support. @@ -39,6 +39,7 @@ #include <wchar.h> #include <limits.h> #include <math.h> +#include <ctype.h> #include "config.h" #include "lib.h" #include "unwind.h" @@ -1315,6 +1316,11 @@ val int_flo(val f) sprintf(text, "%.64g", d); + if (!isdigit(text[0])) + uw_throwf(error_s, + lit("int-flo: cannot convert #<bad-float> to integer"), + nao); + have_exp = (strchr(text, 'e') != 0); have_point = (strchr(text, '.') != 0); @@ -32,6 +32,7 @@ #include <assert.h> #include <setjmp.h> #include <errno.h> +#include <ctype.h> #include <wchar.h> #include <unistd.h> #include "config.h" @@ -1147,6 +1148,12 @@ val vformat(val stream, val fmtstr, va_list vl) sprintf(num_buf, "%.*e", precision, n); else sprintf(num_buf, "%.*f", precision, n); + if (!isdigit(num_buf[0])) { + if (!vformat_str(stream, lit("#<bad-float>"), + width, left, precision)) + return nil; + continue; + } precision = 0; goto output_num; } @@ -1170,6 +1177,13 @@ val vformat(val stream, val fmtstr, va_list vl) case FLNUM: sprintf(num_buf, "%g", obj->fl.n); + if (!isdigit(num_buf[0])) { + if (!vformat_str(stream, lit("#<bad-float>"), + width, left, precision)) + return nil; + continue; + } + if (!precision) { if (!strpbrk(num_buf, "e.")) strcat(num_buf, ".0"); |