summaryrefslogtreecommitdiffstats
path: root/numeric.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-05 09:36:39 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-05 09:36:39 +0000
commit101e79d7b434c01c0e6f4bcc480003858ab8e1a4 (patch)
treea3020e451c67ff06e2b90ad94402e9fc03e2dac1 /numeric.c
parentd75a72e037b5cfb7c6f541b7cd034ccdca700317 (diff)
downloadruby-101e79d7b434c01c0e6f4bcc480003858ab8e1a4.tar.gz
ruby-101e79d7b434c01c0e6f4bcc480003858ab8e1a4.tar.xz
ruby-101e79d7b434c01c0e6f4bcc480003858ab8e1a4.zip
* numeric.c (flo_to_s): keeps enough precision for round trip.
[ruby-core:22325] git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@22783 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/numeric.c b/numeric.c
index 2d6691de6..bb5548566 100644
--- a/numeric.c
+++ b/numeric.c
@@ -521,7 +521,9 @@ rb_float_new(double d)
static VALUE
flo_to_s(VALUE flt)
{
- char buf[32];
+ enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
+ enum {float_dig = DBL_DIG+2};
+ char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
double value = RFLOAT_VALUE(flt);
char *p, *e;
@@ -530,12 +532,12 @@ flo_to_s(VALUE flt)
else if(isnan(value))
return rb_usascii_str_new2("NaN");
- snprintf(buf, sizeof(buf), "%#.15g", value); /* ensure to print decimal point */
+ snprintf(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), "%#.14e", value);
+ snprintf(buf, sizeof(buf), "%#.*e", float_dig - 1, value);
if (!(e = strchr(buf, 'e'))) {
e = buf + strlen(buf);
}