[前][次][番号順一覧][スレッド一覧]

ruby-changes:52394

From: nagachika <ko1@a...>
Date: Sat, 1 Sep 2018 11:31:33 +0900 (JST)
Subject: [ruby-changes:52394] nagachika:r64603 (ruby_2_5): merge revision(s) 64014: [Backport #14926]

nagachika	2018-09-01 11:31:27 +0900 (Sat, 01 Sep 2018)

  New Revision: 64603

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64603

  Log:
    merge revision(s) 64014: [Backport #14926]
    
    fix sum on infinity
    
    * array.c (rb_ary_sum): consider non-finite floats.
      [ruby-core:88024] [Bug #14926]
    
    * enum.c (sum_iter): ditto.

  Modified directories:
    branches/ruby_2_5/
  Modified files:
    branches/ruby_2_5/array.c
    branches/ruby_2_5/enum.c
    branches/ruby_2_5/test/ruby/test_array.rb
    branches/ruby_2_5/test/ruby/test_enum.rb
    branches/ruby_2_5/version.h
Index: ruby_2_5/version.h
===================================================================
--- ruby_2_5/version.h	(revision 64602)
+++ ruby_2_5/version.h	(revision 64603)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_5/version.h#L1
 #define RUBY_VERSION "2.5.2"
 #define RUBY_RELEASE_DATE "2018-09-01"
-#define RUBY_PATCHLEVEL 83
+#define RUBY_PATCHLEVEL 84
 
 #define RUBY_RELEASE_YEAR 2018
 #define RUBY_RELEASE_MONTH 9
Index: ruby_2_5/array.c
===================================================================
--- ruby_2_5/array.c	(revision 64602)
+++ ruby_2_5/array.c	(revision 64603)
@@ -5954,6 +5954,20 @@ rb_ary_sum(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_5/array.c#L5954
             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);
Index: ruby_2_5/test/ruby/test_enum.rb
===================================================================
--- ruby_2_5/test/ruby/test_enum.rb	(revision 64602)
+++ ruby_2_5/test/ruby/test_enum.rb	(revision 64603)
@@ -966,6 +966,21 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/ruby/test_enum.rb#L966
     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
Index: ruby_2_5/test/ruby/test_array.rb
===================================================================
--- ruby_2_5/test/ruby/test_array.rb	(revision 64602)
+++ ruby_2_5/test/ruby/test_array.rb	(revision 64603)
@@ -2956,6 +2956,13 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/ruby/test_array.rb#L2956
     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([]))
Index: ruby_2_5/enum.c
===================================================================
--- ruby_2_5/enum.c	(revision 64602)
+++ ruby_2_5/enum.c	(revision 64603)
@@ -3804,6 +3804,25 @@ sum_iter(VALUE i, struct enum_sum_memo * https://github.com/ruby/ruby/blob/trunk/ruby_2_5/enum.c#L3804
             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);
Index: ruby_2_5
===================================================================
--- ruby_2_5	(revision 64602)
+++ ruby_2_5	(revision 64603)

Property changes on: ruby_2_5
___________________________________________________________________
Modified: svn:mergeinfo
## -0,0 +0,1 ##
   Merged /trunk:r64014

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]