diff --git a/array.c b/array.c index 9061919bf8..c9bf7659df 100644 --- a/array.c +++ b/array.c @@ -5973,6 +5973,20 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary) else goto not_float; + if (isnan(f)) continue; + if (isnan(x)) { + f = x; + continue; + } + if (isinf(x)) { + if (isinf(f) && signbit(x) != signbit(f)) + f = NAN; + else + f = x; + continue; + } + if (isinf(f)) continue; + t = f + x; if (fabs(f) >= fabs(x)) c += ((f - t) + x); diff --git a/enum.c b/enum.c index e57fa5ef46..04b6c9cb95 100644 --- a/enum.c +++ b/enum.c @@ -3809,6 +3809,25 @@ sum_iter(VALUE i, struct enum_sum_memo *memo) goto some_value; } + if (isnan(f)) return; + if (isnan(x)) { + memo->v = i; + memo->f = x; + return; + } + if (isinf(x)) { + if (isinf(f) && signbit(x) != signbit(f)) { + memo->f = NAN; + memo->v = DBL2NUM(f); + } + else { + memo->f = x; + memo->v = i; + } + return; + } + if (isinf(f)) return; + t = f + x; if (fabs(f) >= fabs(x)) c += ((f - t) + x); diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 7a5fbdb766..9962745f1c 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -2967,6 +2967,13 @@ class TestArray < Test::Unit::TestCase assert_float_equal(large_number+(small_number*10), [large_number/1r, *[small_number]*10].sum) assert_float_equal(large_number+(small_number*11), [small_number, large_number/1r, *[small_number]*10].sum) assert_float_equal(small_number, [large_number, small_number, -large_number].sum) + assert_equal(+Float::INFINITY, [+Float::INFINITY].sum) + assert_equal(+Float::INFINITY, [0.0, +Float::INFINITY].sum) + assert_equal(+Float::INFINITY, [+Float::INFINITY, 0.0].sum) + assert_equal(-Float::INFINITY, [-Float::INFINITY].sum) + assert_equal(-Float::INFINITY, [0.0, -Float::INFINITY].sum) + assert_equal(-Float::INFINITY, [-Float::INFINITY, 0.0].sum) + assert_predicate([-Float::INFINITY, Float::INFINITY].sum, :nan?) assert_equal("abc", ["a", "b", "c"].sum("")) assert_equal([1, [2], 3], [[1], [[2]], [3]].sum([])) diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index 12fc2ee66a..2167271886 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -966,6 +966,21 @@ class TestEnumerable < Test::Unit::TestCase assert_float_equal(large_number+(small_number*11), [small_number, large_number/1r, *[small_number]*10].each.sum) assert_float_equal(small_number, [large_number, small_number, -large_number].each.sum) + k = Class.new do + include Enumerable + def initialize(*values) + @values = values + end + def each(&block) + @values.each(&block) + end + end + assert_equal(+Float::INFINITY, k.new(0.0, +Float::INFINITY).sum) + assert_equal(+Float::INFINITY, k.new(+Float::INFINITY, 0.0).sum) + assert_equal(-Float::INFINITY, k.new(0.0, -Float::INFINITY).sum) + assert_equal(-Float::INFINITY, k.new(-Float::INFINITY, 0.0).sum) + assert_predicate(k.new(-Float::INFINITY, Float::INFINITY).sum, :nan?) + assert_equal("abc", ["a", "b", "c"].each.sum("")) assert_equal([1, [2], 3], [[1], [[2]], [3]].each.sum([])) end