summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-09-03 05:37:42 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-09-03 05:37:42 +0000
commit0f35b58a2fbae58a20979de77a3a642c42f41899 (patch)
treecab4099dba0fde8650c94226a1f9ce94549561c9 /numeric.c
parentc9d1be6327640aa5bc01f8c6c8846e4e3fb31337 (diff)
* ruby.c (proc_options): should not alter origargv[].
* ruby.c (set_arg0): long strings for $0 dumped core. * ruby.c (set_arg0): use setprogtitle() if it's available. * io.c (rb_io_popen): accept integer flags as mode. * file.c (rb_find_file_ext): extension table can be supplied from outside. renamed. * eval.c (rb_f_require): replace rb_find_file_noext by rb_find_file_ext. * eval.c (rb_provided): should also check feature without extension. * numeric.c (flo_to_s): do not rely on decimal point to be '.' git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1722 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c37
1 files changed, 19 insertions, 18 deletions
diff --git a/numeric.c b/numeric.c
index c98a0e3fe0..51ff88d7a6 100644
--- a/numeric.c
+++ b/numeric.c
@@ -214,30 +214,31 @@ flo_to_s(flt)
VALUE flt;
{
char buf[24];
- char *s;
+ char *fmt = "%.10g";
double value = RFLOAT(flt)->value;
+ double d1, d2;
if (isinf(value))
return rb_str_new2(value < 0 ? "-Infinity" : "Infinity");
else if(isnan(value))
return rb_str_new2("NaN");
- else
- sprintf(buf, "%-.10g", value);
- if (s = strchr(buf, ' ')) *s = '\0';
- s = buf; if (s[0] == '-') s++;
- if (strchr(s, '.') == 0) {
- int len = strlen(buf);
- char *ind = strchr(buf, 'e');
-
- if (ind) {
- memmove(ind+2, ind, len-(ind-buf)+1);
- ind[0] = '.';
- ind[1] = '0';
- }
- else {
- strcat(buf, ".0");
- }
- }
+
+ if (value < 1.0e-3) {
+ d1 = value;
+ while (d1 < 1.0) d1 *= 10.0;
+ d1 = modf(d1, &d2);
+ if (d1 == 0) fmt = "%.1e";
+ }
+ else if (value >= 1.0e10) {
+ d1 = value;
+ while (d1 > 10.0) d1 /= 10.0;
+ d1 = modf(d1, &d2);
+ if (d1 == 0) fmt = "%.1e";
+ }
+ else if ((d1 = modf(value, &d2)) == 0) {
+ fmt = "%.1f";
+ }
+ sprintf(buf, fmt, value);
return rb_str_new2(buf);
}