summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-04-26 07:41:42 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-04-26 07:41:42 +0000
commit0e68bd16ad4d986527cc890ada2a772383707a1f (patch)
tree9a58078e21b4bd1ac393d940089b2dabdca366bb /sprintf.c
parentcc70bcf9038f0eac57ec1f0becd7b500fbad8686 (diff)
format %f
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sprintf.c')
-rw-r--r--sprintf.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/sprintf.c b/sprintf.c
index 3331fab03a..c657bdef62 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -12,6 +12,9 @@
#include "ruby.h"
#include <ctype.h>
+#include <math.h>
+
+#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
static void fmt_setup _((char*,char,int,int,int));
@@ -553,6 +556,7 @@ f_sprintf(argc, argv)
{
VALUE val = GETARG();
double fval;
+ int i, need = 6;
char fbuf[32];
switch (TYPE(val)) {
@@ -574,8 +578,20 @@ f_sprintf(argc, argv)
}
fmt_setup(fbuf, *p, flags, width, prec);
-
- CHECK(22);
+#if 1
+ need = 0;
+ if (*p != 'e' && *p != 'E') {
+ i = INT_MIN;
+ frexp(fval, &i);
+ if (i > 0)
+ need = BIT_DIGITS(i);
+ }
+ need += (flags&FPREC) ? prec : 6;
+ if ((flags&FWIDTH) && need < width)
+ need = width;
+ need += 20;
+ CHECK(need);
+#endif
sprintf(&buf[blen], fbuf, fval);
blen += strlen(&buf[blen]);
}