Skip to content

Commit 9b52ae2

Browse files
committed
* numeric.c (flo_to_s): keeps enough precision for round trip.
[ruby-core:22325] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22783 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent e691ba3 commit 9b52ae2

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Thu Mar 5 18:36:38 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* numeric.c (flo_to_s): keeps enough precision for round trip.
4+
[ruby-core:22325]
5+
16
Thu Mar 5 16:56:14 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
27

38
* lib/tmpdir.rb (Dir.tmpdir): not use USERPROFILE, and ignores

numeric.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,9 @@ rb_float_new(double d)
521521
static VALUE
522522
flo_to_s(VALUE flt)
523523
{
524-
char buf[32];
524+
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
525+
enum {float_dig = DBL_DIG+2};
526+
char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
525527
double value = RFLOAT_VALUE(flt);
526528
char *p, *e;
527529

@@ -530,12 +532,12 @@ flo_to_s(VALUE flt)
530532
else if(isnan(value))
531533
return rb_usascii_str_new2("NaN");
532534

533-
snprintf(buf, sizeof(buf), "%#.15g", value); /* ensure to print decimal point */
535+
snprintf(buf, sizeof(buf), "%#.*g", float_dig, value); /* ensure to print decimal point */
534536
if (!(e = strchr(buf, 'e'))) {
535537
e = buf + strlen(buf);
536538
}
537539
if (!ISDIGIT(e[-1])) { /* reformat if ended with decimal point (ex 111111111111111.) */
538-
snprintf(buf, sizeof(buf), "%#.14e", value);
540+
snprintf(buf, sizeof(buf), "%#.*e", float_dig - 1, value);
539541
if (!(e = strchr(buf, 'e'))) {
540542
e = buf + strlen(buf);
541543
}

0 commit comments

Comments
 (0)