aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-17 15:27:05 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-17 15:27:05 +0000
commit087a393fa56c4dcf247588d2fb018c4bd46003cb (patch)
tree1a2e9c9d6a2575dcd009d725a17ac3caeef9c179
parentd3e0891423bb92fbc29e8b0e3f0f3aa3258645f2 (diff)
downloadruby-087a393fa56c4dcf247588d2fb018c4bd46003cb.tar.gz
* 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
-rw-r--r--ChangeLog5
-rw-r--r--enum.c66
-rw-r--r--test/ruby/test_enum.rb10
3 files changed, 54 insertions, 27 deletions
diff --git a/ChangeLog b/ChangeLog
index 871ddbf864..08949acd96 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/enum.c b/enum.c
index a459c812f2..3cef60c7ce 100644
--- a/enum.c
+++ b/enum.c
@@ -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++) {
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index e13f6a3f6c..97730f919f 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -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