summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-09-03 05:29:19 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-09-03 05:29:19 +0000
commit4365377d76020c5cacf03a35989f370d05f5780a (patch)
tree3351683d7f4a8991b411dee9dad61f856c42b5e0 /numeric.c
parent9f1e0829daa572708a0aeb3dddbd33d13cb15693 (diff)
* 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 '.' * parse.y (yylex): ternary ? can be followed by newline. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_6@1720 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c36
1 files changed, 19 insertions, 17 deletions
diff --git a/numeric.c b/numeric.c
index a60cb6c364..757667ef09 100644
--- a/numeric.c
+++ b/numeric.c
@@ -208,29 +208,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);
}