From: "H.Yamamoto" Date: 2004-05-17T14:17:53+09:00 Subject: [ruby-dev:23528] Re: [BUG] numeric.c (flo_to_s) broken? 山本です。 printf に "%#.15g" という書式があると知ったので、 組みなおして、大分シンプルになりました。(有効数字15桁、小数点を必ず付加) /* ended with decimal point (111111111111111.) */ のブロックは、p "111111111111111".to_f が "111111111111111." となって 不恰好なので、追加しました。これが問題でなければ、このブロックも 削れると思います。 Index: numeric.c =================================================================== RCS file: /ruby/ruby/numeric.c,v retrieving revision 1.108 diff -u -w -b -p -r1.108 numeric.c --- numeric.c 10 May 2004 08:23:13 -0000 1.108 +++ numeric.c 17 May 2004 05:05:06 -0000 @@ -490,9 +490,7 @@ flo_to_s(flt) VALUE flt; { char buf[32]; - char *fmt = "%.15f"; double value = RFLOAT(flt)->value; - double avalue, d1, d2; char *p, *e; if (isinf(value)) @@ -500,19 +498,20 @@ flo_to_s(flt) else if(isnan(value)) return rb_str_new2("NaN"); - avalue = fabs(value); - if (avalue < 1.0e-7 || avalue >= 1.0e15) { - fmt = "%.15e"; + sprintf(buf, "%#.15g", value); /* ensure to print decimal point */ + if (!(e = strchr(buf, 'e'))) { + e = buf + strlen(buf); } - sprintf(buf, fmt, value); + if (!ISDIGIT(e[-1])) { /* ended with decimal point (111111111111111.) */ + sprintf(buf, "%.14e", value); if (!(e = strchr(buf, 'e'))) { e = buf + strlen(buf); } + } p = e; - while (*--p=='0') - ; - if (*p == '.') *p++; - memmove(p+1, e, strlen(e)+1); + while (p[-1]=='0' && ISDIGIT(p[-2])) + p--; + memmove(p, e, strlen(e)+1); return rb_str_new2(buf); }