ruby-changes:42797
From: mrkn <ko1@a...>
Date: Sun, 1 May 2016 23:06:10 +0900 (JST)
Subject: [ruby-changes:42797] mrkn:r54870 (trunk): Fix rb_ary_sum for mathn
mrkn 2016-05-02 00:02:47 +0900 (Mon, 02 May 2016) New Revision: 54870 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54870 Log: Fix rb_ary_sum for mathn * array.c (rb_ary_sum): fix for mathn * test/ruby/test_array.rb (test_sum): ditto. Modified files: trunk/ChangeLog trunk/array.c trunk/test/ruby/test_array.rb Index: test/ruby/test_array.rb =================================================================== --- test/ruby/test_array.rb (revision 54869) +++ test/ruby/test_array.rb (revision 54870) @@ -2789,6 +2789,10 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2789 assert_equal("abc", ["a", "b", "c"].sum("")) assert_equal([1, [2], 3], [[1], [[2]], [3]].sum([])) + + assert_separately(%w[-rmathn], <<-EOS, ignore_stderr: true) + assert_equal(6, [1r, 2, 3r].sum) + EOS end private Index: array.c =================================================================== --- array.c (revision 54869) +++ array.c (revision 54870) @@ -5732,15 +5732,29 @@ rb_ary_sum(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5732 } if (n != 0) v = rb_fix_plus(LONG2FIX(n), v); - if (r != Qundef) - v = rb_rational_plus(r, v); + if (r != Qundef) { + /* r can be a Integer when mathn is loaded */ + if (FIXNUM_P(r)) + v = rb_fix_plus(r, v); + else if (RB_TYPE_P(r, T_BIGNUM)) + v = rb_big_plus(r, v); + else + v = rb_rational_plus(r, v); + } return v; not_exact: if (n != 0) v = rb_fix_plus(LONG2FIX(n), v); - if (r != Qundef) - v = rb_rational_plus(r, v); + if (r != Qundef) { + /* r can be a Integer when mathn is loaded */ + if (FIXNUM_P(r)) + v = rb_fix_plus(r, v); + else if (RB_TYPE_P(r, T_BIGNUM)) + v = rb_big_plus(r, v); + else + v = rb_rational_plus(r, v); + } if (RB_FLOAT_TYPE_P(e)) { /* Kahan's compensated summation algorithm */ Index: ChangeLog =================================================================== --- ChangeLog (revision 54869) +++ ChangeLog (revision 54870) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun May 1 23:59:59 2016 Kenta Murata <mrkn@m...> + + * array.c (rb_ary_sum): fix for mathn + + * test/ruby/test_array.rb (test_sum): ditto. + Sun May 1 23:51:54 2016 NAKAMURA Usaku <usa@r...> * test/lib/test/unit.rb (Options#non_options): fixed wrong regexp. -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/