summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'sprintf.c')
-rw-r--r--sprintf.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/sprintf.c b/sprintf.c
index 0370453f09..c3a2772e67 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 */
#ifndef atof
double strtod();
@@ -562,6 +565,7 @@ rb_f_sprintf(argc, argv)
{
VALUE val = GETARG();
double fval;
+ int i, need = 6;
char fbuf[32];
switch (TYPE(val)) {
@@ -583,8 +587,19 @@ rb_f_sprintf(argc, argv)
}
fmt_setup(fbuf, *p, flags, width, prec);
+ 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(22);
+ CHECK(need);
sprintf(&buf[blen], fbuf, fval);
blen += strlen(&buf[blen]);
}