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

ruby-changes:42528

From: mrkn <ko1@a...>
Date: Fri, 15 Apr 2016 22:50:00 +0900 (JST)
Subject: [ruby-changes:42528] mrkn:r54602 (trunk): array.c (rb_ary_sum): use rb_rational_add directly

mrkn	2016-04-15 23:46:35 +0900 (Fri, 15 Apr 2016)

  New Revision: 54602

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

  Log:
    array.c (rb_ary_sum): use rb_rational_add directly
    
    * rational.c (rb_rational_add): rename from nurat_add.
    
    * array.c (rb_ary_sum): use rb_rational_add directly.
    
    * test/ruby/test_array.rb (test_sum): add assertions for an array of
      Rational values.

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/internal.h
    trunk/rational.c
    trunk/test/ruby/test_array.rb
Index: array.c
===================================================================
--- array.c	(revision 54601)
+++ array.c	(revision 54602)
@@ -5762,7 +5762,7 @@ rb_ary_sum(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5762
                 v = rb_to_float(v);
                 goto float_value;
             }
-            v = rb_funcall(v, idPLUS, 1, e);
+            v = rb_rational_add(e, v);
         }
         return v;
     }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54601)
+++ ChangeLog	(revision 54602)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Apr 15 23:42:00 2016  Kenta Murata  <mrkn@m...>
+
+	* rational.c (rb_rational_add): rename from nurat_add.
+
+	* array.c (rb_ary_sum): use rb_rational_add directly.
+
+	* test/ruby/test_array.rb (test_sum): add assertions for an array of
+	  Rational values.
+
 Fri Apr 15 22:31:00 2016  Kenta Murata  <mrkn@m...>
 
 	* array.c (rb_ary_sum): apply the precision compensated algorithm
Index: internal.h
===================================================================
--- internal.h	(revision 54601)
+++ internal.h	(revision 54602)
@@ -1204,6 +1204,7 @@ rb_pid_t rb_fork_ruby(int *status); https://github.com/ruby/ruby/blob/trunk/internal.h#L1204
 void rb_last_status_clear(void);
 
 /* rational.c */
+VALUE rb_rational_add(VALUE self, VALUE other);
 VALUE rb_lcm(VALUE x, VALUE y);
 VALUE rb_rational_reciprocal(VALUE x);
 VALUE rb_cstr_to_rat(const char *, int);
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb	(revision 54601)
+++ test/ruby/test_array.rb	(revision 54602)
@@ -2761,6 +2761,8 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2761
     assert_float_equal((FIXNUM_MAX+1).to_f, [FIXNUM_MAX, 1, 0.0].sum)
     assert_float_equal((FIXNUM_MAX+1).to_f, [0.0, FIXNUM_MAX+1].sum)
 
+    assert_rational_equal(5/6r, [1/2r, 1/3r].sum)
+
     assert_equal(2.0+3.0i, [2.0, 3.0i].sum)
 
     assert_int_equal(13, [1, 2].sum(10))
Index: rational.c
===================================================================
--- rational.c	(revision 54601)
+++ rational.c	(revision 54602)
@@ -741,8 +741,8 @@ f_addsub(VALUE self, VALUE anum, VALUE a https://github.com/ruby/ruby/blob/trunk/rational.c#L741
  *    Rational(9, 8)  + 4                #=> (41/8)
  *    Rational(20, 9) + 9.8              #=> 12.022222222222222
  */
-static VALUE
-nurat_add(VALUE self, VALUE other)
+VALUE
+rb_rational_add(VALUE self, VALUE other)
 {
     if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
 	{
@@ -2538,7 +2538,7 @@ Init_Rational(void) https://github.com/ruby/ruby/blob/trunk/rational.c#L2538
     rb_define_method(rb_cRational, "numerator", nurat_numerator, 0);
     rb_define_method(rb_cRational, "denominator", nurat_denominator, 0);
 
-    rb_define_method(rb_cRational, "+", nurat_add, 1);
+    rb_define_method(rb_cRational, "+", rb_rational_add, 1);
     rb_define_method(rb_cRational, "-", nurat_sub, 1);
     rb_define_method(rb_cRational, "*", nurat_mul, 1);
     rb_define_method(rb_cRational, "/", nurat_div, 1);

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

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