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

ruby-changes:42163

From: mrkn <ko1@a...>
Date: Wed, 23 Mar 2016 21:41:05 +0900 (JST)
Subject: [ruby-changes:42163] mrkn:r54237 (trunk): * enum.c (ary_inject_op): Use Kahan's compensated summation algorithm

mrkn	2016-03-23 21:41:00 +0900 (Wed, 23 Mar 2016)

  New Revision: 54237

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

  Log:
    * enum.c (ary_inject_op): Use Kahan's compensated summation algorithm
      for summing up float values.

  Modified files:
    trunk/ChangeLog
    trunk/enum.c
    trunk/test/ruby/test_enum.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54236)
+++ ChangeLog	(revision 54237)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Mar 23 21:38:00 2016  Kenta Murata  <mrkn@m...>
+
+	* enum.c (ary_inject_op): Use Kahan's compensated summation algorithm
+	  for summing up float values.
+
 Wed Mar 23 20:56:59 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* strftime.c (rb_strftime_with_timespec): append formatted results
Index: test/ruby/test_enum.rb
===================================================================
--- test/ruby/test_enum.rb	(revision 54236)
+++ test/ruby/test_enum.rb	(revision 54237)
@@ -217,6 +217,13 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L217
     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(:+))
+
+    large_number = 100000000
+    small_number = 1e-9
+    until (large_number + small_number) == large_number
+      small_number /= 10
+    end
+    assert_equal(large_number+(small_number*10), [large_number, *[small_number]*10].inject(:+))
   end
 
   def test_inject_array_plus_redefined
Index: enum.c
===================================================================
--- enum.c	(revision 54236)
+++ enum.c	(revision 54237)
@@ -634,7 +634,7 @@ ary_inject_op(VALUE ary, VALUE init, VAL https://github.com/ruby/ruby/blob/trunk/enum.c#L634
     ID id;
     VALUE v, e;
     long i, n;
-    double f;
+    double f, c;
 
     if (RARRAY_LEN(ary) == 0)
         return init == Qundef ? Qnil : init;
@@ -686,16 +686,23 @@ ary_inject_op(VALUE ary, VALUE init, VAL https://github.com/ruby/ruby/blob/trunk/enum.c#L686
                  rb_method_basic_definition_p(rb_cFloat, idPLUS)) {
             f = RFLOAT_VALUE(v);
           sum_float:
+            c = 0.0;
             while (1) {
+                double y, t;
                 e = RARRAY_AREF(ary, i);
                 if (RB_FLOAT_TYPE_P(e))
-                    f += RFLOAT_VALUE(e);
+                    y = RFLOAT_VALUE(e) - c;
                 else if (FIXNUM_P(e))
-                    f += FIX2LONG(e);
+                    y = FIX2LONG(e) - c;
                 else if (RB_TYPE_P(e, T_BIGNUM))
-                    f += rb_big2dbl(e);
+                    y = rb_big2dbl(e) - c;
                 else
                     break;
+
+                t = f + y;
+                c = (t - f) - y;
+                f = t;
+
                 i++;
                 if (RARRAY_LEN(ary) <= i)
                     return DBL2NUM(f);

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

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