summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--numeric.c21
2 files changed, 19 insertions, 11 deletions
diff --git a/ChangeLog b/ChangeLog
index 00d44bfe2a..7c822e8f4e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Mon May 17 16:04:06 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp>
+
+ * 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')
+
Mon May 17 00:36:21 2004 why the lucky stiff <why@ruby-lang.org>
* lib/yaml/baseemitter.rb (indent_text): was forcing a mod value
diff --git a/numeric.c b/numeric.c
index e3afca6ee3..e0eab93f56 100644
--- a/numeric.c
+++ b/numeric.c
@@ -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, 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);
}