mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* numeric.c (flo_to_s): make minimum string representation.
[ruby-core:30145] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27745 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7abe8a5560
commit
07b314f07b
3 changed files with 55 additions and 16 deletions
|
@ -1,3 +1,8 @@
|
|||
Wed May 12 10:57:04 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* numeric.c (flo_to_s): make minimum string representation.
|
||||
[ruby-core:30145]
|
||||
|
||||
Wed May 12 09:21:05 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
||||
|
||||
* re.c (rb_reg_initialize_m): fix wrong index for the lang
|
||||
|
|
59
numeric.c
59
numeric.c
|
@ -558,35 +558,62 @@ rb_float_new(double d)
|
|||
static VALUE
|
||||
flo_to_s(VALUE flt)
|
||||
{
|
||||
char *ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve);
|
||||
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
|
||||
enum {float_dig = DBL_DIG+1};
|
||||
char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
|
||||
double value = RFLOAT_VALUE(flt);
|
||||
VALUE s;
|
||||
char *p, *e;
|
||||
int sign, decpt, digs;
|
||||
|
||||
if (isinf(value))
|
||||
return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity");
|
||||
else if (isnan(value))
|
||||
return rb_usascii_str_new2("NaN");
|
||||
|
||||
# define FLOFMT(buf, size, fmt, prec, val) snprintf(buf, size, fmt, prec, val), \
|
||||
(void)((atof(buf) == val) || snprintf(buf, size, fmt, (prec)+1, val))
|
||||
|
||||
FLOFMT(buf, sizeof(buf), "%#.*g", float_dig, 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.) */
|
||||
FLOFMT(buf, sizeof(buf), "%#.*e", float_dig - 1, value);
|
||||
if (!(e = strchr(buf, 'e'))) {
|
||||
e = buf + strlen(buf);
|
||||
p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
|
||||
s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0);
|
||||
if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1;
|
||||
memcpy(buf, p, digs);
|
||||
xfree(p);
|
||||
if (decpt > 0) {
|
||||
if (decpt < digs) {
|
||||
memmove(buf + decpt + 1, buf + decpt, digs - decpt);
|
||||
buf[decpt] = '.';
|
||||
rb_str_cat(s, buf, digs + 1);
|
||||
}
|
||||
else if (decpt - digs < float_dig) {
|
||||
rb_str_cat(s, buf, digs);
|
||||
rb_str_cat(s, ".0", 2);
|
||||
}
|
||||
else {
|
||||
goto exp;
|
||||
}
|
||||
}
|
||||
p = e;
|
||||
while (p[-1]=='0' && ISDIGIT(p[-2]))
|
||||
p--;
|
||||
memmove(p, e, strlen(e)+1);
|
||||
return rb_usascii_str_new2(buf);
|
||||
else if (decpt > -4) {
|
||||
long len;
|
||||
char *ptr;
|
||||
rb_str_cat(s, "0.", 2);
|
||||
rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs);
|
||||
ptr = RSTRING_PTR(s);
|
||||
memset(ptr += len, '0', -decpt);
|
||||
memcpy(ptr -= decpt, buf, digs);
|
||||
}
|
||||
else {
|
||||
exp:
|
||||
if (digs > 1) {
|
||||
memmove(buf + 2, buf + 1, digs - 1);
|
||||
}
|
||||
else {
|
||||
buf[2] = '0';
|
||||
digs++;
|
||||
}
|
||||
buf[1] = '.';
|
||||
rb_str_cat(s, buf, digs + 1);
|
||||
rb_str_catf(s, "e%+d", decpt - 1);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -136,6 +136,13 @@ class TestFloat < Test::Unit::TestCase
|
|||
assert_equal("NaN", (inf / inf).to_s)
|
||||
|
||||
assert_equal("1.0e+18", 1000_00000_00000_00000.0.to_s)
|
||||
|
||||
bug3273 = '[ruby-core:30145]'
|
||||
[0.21611564636388508, 0.56].each do |f|
|
||||
s = f.to_s
|
||||
assert_equal(f, s.to_f, bug3273)
|
||||
assert_not_equal(f, s.chop.to_f, bug3273)
|
||||
end
|
||||
end
|
||||
|
||||
def test_coerce
|
||||
|
|
Loading…
Add table
Reference in a new issue