summaryrefslogtreecommitdiffstats
path: root/numeric.c
diff options
context:
space:
mode:
authorocean <ocean@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-05-17 07:14:45 +0000
committerocean <ocean@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-05-17 07:14:45 +0000
commit82f12c621f5991a290cfe07c0bf4b4c605a1d1aa (patch)
tree7bd0475b6950957f2d50e9fc3b0b2057870f8b62 /numeric.c
parent76089e03920e67f8d6a3d7276720d68afbc28863 (diff)
downloadruby-82f12c621f5991a290cfe07c0bf4b4c605a1d1aa.tar.gz
ruby-82f12c621f5991a290cfe07c0bf4b4c605a1d1aa.tar.xz
ruby-82f12c621f5991a290cfe07c0bf4b4c605a1d1aa.zip
* numeric.c (flo_to_s): p 0.0 should be '0.0' not '0.0e+00'.
* numeric.c (flo_to_s): the number of significand is correctly handled, there is assumption that DBL_DIG == 15 though. (p 0.00000000000000000001 was '9.999999999999999e-21', now is '1.0e-20') git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8@6346 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c21
1 files changed, 10 insertions, 11 deletions
diff --git a/numeric.c b/numeric.c
index ac5e125a8..2d33b49ba 100644
--- a/numeric.c
+++ b/numeric.c
@@ -489,9 +489,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))
@@ -499,19 +497,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 = "%.16e";
- }
- sprintf(buf, fmt, value);
+ sprintf(buf, "%#.15g", 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.) */
+ 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);
}