summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-04-06 16:08:23 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-04-06 16:08:23 +0000
commit8ebd0d4320a633c520eb94fe4a31e6dc16e4ce42 (patch)
tree6b9e01458da86f90fc42ab6987d7bdf82a65df50 /numeric.c
parentaa75b002de21dfeb4ab0f3ca9bc0d50a3dd5b65c (diff)
* numeric.c (flo_to_s): reduce fragments if no precision lost.
c.f. [ruby-core:23075] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23144 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/numeric.c b/numeric.c
index dad085e890..5b051f1452 100644
--- a/numeric.c
+++ b/numeric.c
@@ -522,7 +522,7 @@ static VALUE
flo_to_s(VALUE flt)
{
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
- enum {float_dig = DBL_DIG+2};
+ enum {float_dig = DBL_DIG+1};
char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
double value = RFLOAT_VALUE(flt);
char *p, *e;
@@ -532,12 +532,15 @@ flo_to_s(VALUE flt)
else if(isnan(value))
return rb_usascii_str_new2("NaN");
- snprintf(buf, sizeof(buf), "%#.*g", float_dig, value); /* ensure to print decimal point */
+# define FLOFMT(buf, size, fmt, prec, val) snprintf(buf, size, fmt, prec, val), \
+ (void)((atof(buf) == val) || snprintf(buf, size, fmt, (prec)+1, val))
+
+ FLOFMT(buf, sizeof(buf), "%#.*g", float_dig, value); /* ensure to print decimal point */
if (!(e = strchr(buf, 'e'))) {
e = buf + strlen(buf);
}
if (!ISDIGIT(e[-1])) { /* reformat if ended with decimal point (ex 111111111111111.) */
- snprintf(buf, sizeof(buf), "%#.*e", float_dig - 1, value);
+ FLOFMT(buf, sizeof(buf), "%#.*e", float_dig - 1, value);
if (!(e = strchr(buf, 'e'))) {
e = buf + strlen(buf);
}