1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* enum.c (ary_inject_op): Implement the specialized code for sum of

float numbers.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54162 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2016-03-17 15:27:05 +00:00
parent d3e0891423
commit 087a393fa5
3 changed files with 54 additions and 27 deletions

View file

@ -1,3 +1,8 @@
Fri Mar 18 00:25:56 2016 Tanaka Akira <akr@fsij.org>
* enum.c (ary_inject_op): Implement the specialized code for sum of
float numbers.
Fri Mar 18 00:15:05 2016 Yusuke Endoh <mame@ruby-lang.org>
* numeric.c (num_step): use rb_equal for zero check. rb_num_coerce_cmp

66
enum.c
View file

@ -632,8 +632,9 @@ static VALUE
ary_inject_op(VALUE ary, VALUE init, VALUE op)
{
ID id;
VALUE v;
long i;
VALUE v, e;
long i, n;
double f;
if (RARRAY_LEN(ary) == 0)
return init == Qundef ? Qnil : init;
@ -641,6 +642,8 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
if (init == Qundef) {
v = RARRAY_AREF(ary, 0);
i = 1;
if (RARRAY_LEN(ary) == 1)
return v;
}
else {
v = init;
@ -649,43 +652,54 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
id = SYM2ID(op);
if (id == idPLUS) {
if (FIXNUM_P(v) &&
rb_method_basic_definition_p(rb_cFixnum, idPLUS)) {
long n = FIX2LONG(v);
while (i < RARRAY_LEN(ary)) {
VALUE e = RARRAY_AREF(ary, i);
if (!FIXNUM_P(e)) break;
n += FIX2LONG(e); /* should not overflow long type */
i++;
if (!FIXABLE(n)) break;
}
v = LONG2NUM(n);
}
if (i < RARRAY_LEN(ary) && (FIXNUM_P(v) || RB_TYPE_P(v, T_BIGNUM)) &&
rb_method_basic_definition_p(rb_cFixnum, idPLUS) &&
rb_method_basic_definition_p(rb_cBignum, idPLUS)) {
long n = 0;
while (i < RARRAY_LEN(ary)) {
VALUE e = RARRAY_AREF(ary, i);
if ((FIXNUM_P(v) || RB_TYPE_P(v, T_BIGNUM)) &&
rb_method_basic_definition_p(rb_cFixnum, idPLUS) &&
rb_method_basic_definition_p(rb_cBignum, idPLUS)) {
n = 0;
while (1) {
e = RARRAY_AREF(ary, i);
if (FIXNUM_P(e)) {
n += FIX2LONG(e); /* should not overflow long type */
i++;
if (!FIXABLE(n)) {
v = rb_big_plus(LONG2NUM(n), v);
n = 0;
}
}
else if (RB_TYPE_P(e, T_BIGNUM)) {
else if (RB_TYPE_P(e, T_BIGNUM))
v = rb_big_plus(e, v);
i++;
}
else {
else
break;
}
i++;
if (RARRAY_LEN(ary) <= i)
return n == 0 ? v : rb_fix_plus(LONG2FIX(n), v);
}
if (n != 0) {
v = rb_fix_plus(LONG2FIX(n), v);
}
if (RB_FLOAT_TYPE_P(e) &&
rb_method_basic_definition_p(rb_cFloat, idPLUS)) {
f = NUM2DBL(v);
goto sum_float;
}
}
else if (RB_FLOAT_TYPE_P(v) &&
rb_method_basic_definition_p(rb_cFloat, idPLUS)) {
f = RFLOAT_VALUE(v);
sum_float:
while (1) {
e = RARRAY_AREF(ary, i);
if (RB_FLOAT_TYPE_P(e))
f += RFLOAT_VALUE(e);
else if (FIXNUM_P(e))
f += FIX2LONG(e);
else if (RB_TYPE_P(e, T_BIGNUM))
f += rb_big2dbl(e);
else
break;
i++;
if (RARRAY_LEN(ary) <= i)
return DBL2NUM(f);
}
}
}
for (; i<RARRAY_LEN(ary); i++) {

View file

@ -197,11 +197,16 @@ class TestEnumerable < Test::Unit::TestCase
assert_equal(105, [5, 7].inject(3, :*))
end
def assert_float_equal(e, v, msg=nil)
assert_equal(Float, v.class, msg)
assert_equal(e, v, msg)
end
def test_inject_array_plus
assert_equal(3, [3].inject(:+))
assert_equal(8, [3, 5].inject(:+))
assert_equal(15, [3, 5, 7].inject(:+))
assert_equal(15.0, [3, 5, 7.0].inject(:+))
assert_float_equal(15.0, [3, 5, 7.0].inject(:+))
assert_equal(2*FIXNUM_MAX, Array.new(2, FIXNUM_MAX).inject(:+))
assert_equal(2*(FIXNUM_MAX+1), Array.new(2, FIXNUM_MAX+1).inject(:+))
assert_equal(10*FIXNUM_MAX, Array.new(10, FIXNUM_MAX).inject(:+))
@ -209,6 +214,9 @@ class TestEnumerable < Test::Unit::TestCase
assert_equal(FIXNUM_MAX*10, ([FIXNUM_MAX+1, -1]*10).inject(:+))
assert_equal(2*FIXNUM_MIN, Array.new(2, FIXNUM_MIN).inject(:+))
assert_equal((FIXNUM_MAX+1).to_f, [FIXNUM_MAX, 1, 0.0].inject(:+))
assert_float_equal(10.0, [3.0, 5].inject(2.0, :+))
assert_float_equal((FIXNUM_MAX+1).to_f, [0.0, FIXNUM_MAX+1].inject(:+))
assert_equal(2.0+3.0i, [2.0, 3.0i].inject(:+))
end
def test_inject_array_plus_redefined