Skip to content

Commit

Permalink
* numeric.c (flo_to_s): keeps enough precision for round trip.
Browse files Browse the repository at this point in the history
  [ruby-core:22325]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22783 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Mar 5, 2009
1 parent e691ba3 commit 9b52ae2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
@@ -1,3 +1,8 @@
Thu Mar 5 18:36:38 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>

* numeric.c (flo_to_s): keeps enough precision for round trip.
[ruby-core:22325]

Thu Mar 5 16:56:14 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>

* lib/tmpdir.rb (Dir.tmpdir): not use USERPROFILE, and ignores
Expand Down
8 changes: 5 additions & 3 deletions numeric.c
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down

0 comments on commit 9b52ae2

Please sign in to comment.