Skip to content

Commit

Permalink
Fix rb_ary_sum for mathn
Browse files Browse the repository at this point in the history
* array.c (rb_ary_sum): fix for mathn

* test/ruby/test_array.rb (test_sum): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54870 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
mrkn committed May 1, 2016
1 parent b137661 commit 78729a5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Sun May 1 23:59:59 2016 Kenta Murata <mrkn@mrkn.jp>

* 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@ruby-lang.org>

* test/lib/test/unit.rb (Options#non_options): fixed wrong regexp.
Expand Down
22 changes: 18 additions & 4 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -5732,15 +5732,29 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary)
}
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 */
Expand Down
4 changes: 4 additions & 0 deletions test/ruby/test_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,10 @@ def test_sum

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
Expand Down

0 comments on commit 78729a5

Please sign in to comment.